<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">Blog posts by Tanuj Joshi</title><link href="http://world.optimizely.com" /><updated>2018-02-20T18:30:02.0000000Z</updated><id>https://world.optimizely.com/blogs/tanuj-joshi/</id> <generator uri="http://world.optimizely.com" version="2.0">Optimizely World</generator> <entry><title>Make OWIN PCI Compliant using cookie authentication timeouts (ValidateInterval vs ExpireTimeSpan]</title><link href="https://world.optimizely.com/blogs/tanuj-joshi/dates/2018/2/make-owin-pci-compliant-using-cookie-authentication-timeouts-validateinterval-vs-expiretimespan/" /><id>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;Let&amp;rsquo;s talk about PCI first,&lt;/p&gt;
&lt;p&gt;In order to make login PCI compliant, session timeout needs to be set for 15 mins, I had to make two changes to my Startup.cs file.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Set &lt;em&gt;SlidingExpiration to False&lt;/em&gt;. Sliding Expiration is set to true by default. [This is optional and depends on requirements.]&lt;/li&gt;
&lt;li&gt;****Add &lt;em&gt;ExpireTimeSpan to 15 mins&lt;/em&gt;. ExpireTimeSpan field by default is 14 days.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If you are using cookie authentication in ASP.NET Identity, there are two timeout settings that may look very similar,&amp;nbsp;&lt;strong&gt;ValidateInterval&amp;nbsp;&lt;/strong&gt;and&amp;nbsp;&lt;strong&gt;ExpireTimespan&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is ExpireTimeSpan&lt;/strong&gt;?&lt;/p&gt;
&lt;p&gt;ExpireTimeSpan allows you to set how long the issued cookie is valid for. In the code sample below, the cookie is valid for 15 minutes from the time of creation. Once those 15 minutes are up the user will have to sign-in because the&amp;nbsp;SlidingExpiration&amp;nbsp;is set to&amp;nbsp;false.&lt;/p&gt;
&lt;p&gt;However, let&amp;rsquo;s suppose, Sliding expiration&amp;nbsp;is true [by default]. What would happen then?&lt;/p&gt;
&lt;p&gt;The cookie would be&amp;nbsp;&lt;span style=&quot;text-decoration: underline;&quot;&gt;regenerated&lt;/span&gt;&amp;nbsp;on any request within 15 mins. For example, if the user logged in and subsequently made a second request 5 minutes later the cookie would be &lt;span style=&quot;text-decoration: underline;&quot;&gt;regenerated&lt;/span&gt;&amp;nbsp;for another 15 minutes. If the user logged in and then made a second request at 16th min or later, only then, the user would be prompted to log in.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is ValidateInterval [this can be tricky]:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In order to understand ValidateInterval, let&amp;rsquo;s talk about Security stamp first. A Security stamp for a user is created/updated every time a password is created/changed or an external login is added/removed. Every time a user logs in, SecurityStampValidator.OnValidateIdentity validates the security stamp using the cookie. And now, if the user has changed a password, the cookie becomes invalid next time.&lt;/p&gt;
&lt;p&gt;The&amp;nbsp;validateInterval&amp;nbsp;attribute of the&amp;nbsp;SecurityStampValidator.OnValidateIdentity checks the security stamp to ensure the validity of the cookie after the given interval. This is different than ExpireTimeSpan.However, the end result will&amp;nbsp;be same Logged out state.&lt;/p&gt;
&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt; // Use cookie authentication
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString(&quot;/Login&quot;),
                Provider = new CookieAuthenticationProvider
                {
                    // If the &quot;/util/login.aspx&quot; has been used for login otherwise you don&#39;t need it you can remove OnApplyRedirect.
                    OnApplyRedirect = cookieApplyRedirectContext =&amp;gt;
                    {
                        app.CmsOnCookieApplyRedirect(cookieApplyRedirectContext, cookieApplyRedirectContext.OwinContext.Get&amp;lt;ApplicationSignInManager&amp;lt;ApplicationUser&amp;gt;&amp;gt;());
                    },

                    // Enables the application to validate the security stamp when the user logs in.
                    // This is a security feature which is used when you change a password or add an external login to your account.
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity&amp;lt;ApplicationUserManager&amp;lt;ApplicationUser&amp;gt;, ApplicationUser&amp;gt;(
                        validateInterval: TimeSpan.FromMinutes(15),
                        regenerateIdentity: (manager, user) =&amp;gt; manager.GenerateUserIdentityAsync(user))
                },
                SlidingExpiration = false,
                ExpireTimeSpan = TimeSpan.FromMinutes(15)
            });&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In above example, both timeouts are set to 15 mins. Ideally, ValidateInterval should be set less than ExpireTimeSpan. This is because, once ExpireTimeSpan is reached, the user will automatically get re-validated upon next login request.&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;</id><updated>2018-02-20T18:30:02.0000000Z</updated><summary type="html">Blog post</summary></entry> <entry><title>Extending EPiServerLog.config </title><link href="https://world.optimizely.com/blogs/tanuj-joshi/dates/2018/2/extending-episerverlog-config-/" /><id>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h3&gt;Log custom errors or success/fail information for REST APIs, Web services etc using EPiServer Log4net namespace [EPiServer.Logging]&lt;/h3&gt;
&lt;p&gt;Step 1: Add your custom Logging information to EpiserverLog.config&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a. Add your Appender&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Sample &amp;nbsp;File Appender:&lt;/p&gt;
&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    &amp;lt;!--Appender for MyCustomLog using File Appender --&amp;gt;
    &amp;lt;appender name=&quot;MyCustomLog&quot; type=&quot;log4net.Appender.FileAppender&quot;&amp;gt;
        &amp;lt;!--File name and Location--&amp;gt;
        &amp;lt;file value=&quot;App_Data\MyCustomLog.log&quot; /&amp;gt;
        &amp;lt;!--Indicates whether the file should be appended to or overwritten.--&amp;gt;
        &amp;lt;appendToFile value=&quot;true&quot; /&amp;gt;
        &amp;lt;!--Encoding Type--&amp;gt;
        &amp;lt;encoding value=&quot;utf-8&quot; /&amp;gt;
        &amp;lt;!--true if always should be logged to the same file, otherwise false.--&amp;gt;
        &amp;lt;staticLogFileName value=&quot;true&quot;/&amp;gt;
        &amp;lt;!--Log all information--&amp;gt;
        &amp;lt;threshold value=&quot;All&quot; /&amp;gt;
        &amp;lt;!--Types of Locking: https://logging.apache.org/log4net/log4net-1.2.13/release/sdk/log4net.Appender.FileAppender.LockingModel.html--&amp;gt;
        &amp;lt;lockingModel type=&quot;log4net.Appender.FileAppender+MinimalLock&quot; /&amp;gt;
        &amp;lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&amp;gt;
            &amp;lt;conversionPattern value=&quot;%date %message%newline&quot; /&amp;gt;
        &amp;lt;/layout&amp;gt;
    &amp;lt;/appender&amp;gt;
    &amp;lt;!--Appender for MyCustomLog data--&amp;gt;

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; b.&amp;nbsp;Add Logger tag right after &amp;lt;/root&amp;gt; &amp;amp; before &amp;lt;/log4net&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;&amp;lt;!--Separate Logger for MyCustomLog --&amp;gt;
 &amp;lt;logger additivity=&quot;false&quot; name=&quot;MyCustomLog&quot;&amp;gt;
        &amp;lt;level value=&quot;All&quot;/&amp;gt;
        &amp;lt;!--Your Appender Name--&amp;gt;
        &amp;lt;appender-ref ref=&quot;MyCustomLog&quot; /&amp;gt;
 &amp;lt;/logger&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Step 2: Write to your log&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;EPiServer.Logging.ILogger mylog = EPiServer.Logging.LogManager.Instance.GetLogger(&quot;MyCustomLog&quot;);
// Appender name: &quot;MyCustomLog&quot;

 mylog.Info($&quot;This is test data.&quot;);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Preview:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;img src=&quot;/link/a0d20bd0f88a4a41a5eff7c8af7df303.aspx&quot; alt=&quot;Image 111.bmp&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Different Appender types available:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Write to File: [&lt;em&gt;log4net.Appender.FileAppender&lt;/em&gt;] - Write to a file.&lt;/li&gt;
&lt;li&gt;Write To Rolling File: [&lt;em&gt;log4net.Appender.RollingFileAppender&lt;/em&gt;] - Rolling Style used by EPiServer.&lt;/li&gt;
&lt;li&gt;Database log: [&lt;em&gt;log4net.Appender.AdoNetAppender&lt;/em&gt;] - write logs to database.&lt;/li&gt;
&lt;li&gt;Write to Event log: [&lt;em&gt;log4net.Appender.EventLogAppender&lt;/em&gt;] - Write events to event log.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;More Info Here:&amp;nbsp;&lt;a href=&quot;/link/c09626448b6648d6b1a6f660d7abba65.aspx&quot;&gt;https://world.episerver.com/documentation/developer-guides/CMS/logging/&lt;/a&gt;&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;</id><updated>2018-02-01T20:00:56.0000000Z</updated><summary type="html">Blog post</summary></entry></feed>