4

The Configuration method of OWIN startup class is called twice in asp.net identity application. The method is called twice i.e. before and after page load on server side. I already found one question(Why is the Configuration method of the SignalR Startup class invoked twice) related to it but it doesn't clarify me regarding what is pre-initialization and post-initialization of code. Why it is needed and will it have any performance issues in my application??

Here the the code which is running twice :

 public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }

Below is the implementation of configuration method :

 public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context, user manager and signin manager to use a single instance per request
        app.CreatePerOwinContext(RavenContext.CreateSession);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        // Configure the sign in cookie
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                // 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<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });            
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
Community
  • 1
  • 1
Shahbaz Khan
  • 133
  • 1
  • 10

1 Answers1

0

I'm not convinced it is loading before and after a page but I had a similar situation where I had a virtual directory /preview that pointed to the same folder as the root.

When I hit F5 both /preview and the / applications were instantiated - resulting in two calls to Startup.Configuration.

I just deleted /preview virtual directory and then it was only called once.

It can be difficult to put a breakpoint in Startup so try something like this to see exactly how many times the startup is initialized.

public class Startup
{
    public static Guid GlobalGuid { get; private set; }

    static Startup()
    {
        GlobalGuid = Guid.NewGuid();
    }

    public void Configuration(IAppBuilder app)
    {           
       File.AppendAllLines(@"c:\temp\Startup.txt", 
                           new[] { DateTime.Now + " initialized - " + GlobalGuid });
    }
}
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689