为什么在页面加载之前和之后调用Owin Startup类的Configuration方法?

时间:2015-05-25 08:19:39

标签: asp.net asp.net-mvc asp.net-identity owin

在asp.net身份应用程序中调用OWIN启动类的Configuration方法两次。该方法被调用两次,即在服务器端的页面加载之前和之后。 我已经发现了一个与之相关的问题(see),但它并没有澄清我对代码的预初始化和后初始化的要求。为什么需要它,我的应用程序中是否会出现任何性能问题?

这里是运行两次的代码:

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

以下是配置方法的实现:

 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);
}

1 个答案:

答案 0 :(得分:0)

我不相信它是在页面之前和之后加载,但我遇到类似的情况,我有一个虚拟目录/preview指向与根目录相同的文件夹。

当我点击F5时,/preview/个应用程序都被实例化了 - 导致两次调用Startup.Configuration

我刚刚删除了/preview虚拟目录,然后只调用了一次。

在Startup中设置断点可能很困难,所以尝试这样的方法来确切地了解启动初始化的次数。

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 });
    }
}