Asp.net 为什么在页面加载前后调用Owin启动类的配置方法?

Asp.net 为什么在页面加载前后调用Owin启动类的配置方法?,asp.net,asp.net-mvc,asp.net-identity,owin,Asp.net,Asp.net Mvc,Asp.net Identity,Owin,在asp.net identity应用程序中,OWIN启动类的配置方法被调用两次。该方法被调用两次,即在服务器端页面加载之前和之后。 我已经找到了一个与之相关的问题(),但它并没有澄清什么是代码的初始化前和初始化后。为什么需要它?我的应用程序中是否会出现性能问题 下面是运行两次的代码: public partial class Startup { public void Configuration(IAppBuilder app) {

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

下面是运行两次的代码:

 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);
}
public void ConfigureAuth(IAppBuilder应用程序)
{
//将数据库上下文、用户管理器和登录管理器配置为每个请求使用一个实例
app.CreatePerOwinContext(RavenContext.CreateSession);
app.CreatePerOwinContext(ApplicationUserManager.Create);
app.CreatePerOwinContext(ApplicationSignInManager.Create);
//使应用程序能够使用cookie存储登录用户的信息
//以及使用cookie临时存储用户登录第三方登录提供商的信息
//配置登录cookie
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
LoginPath=新路径字符串(“/Account/Login”),
Provider=新CookieAuthenticationProvider
{
//允许应用程序在用户登录时验证安全戳。
//这是一种安全功能,在您更改密码或向帐户添加外部登录时使用。
OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
validateInterval:TimeSpan.FromMinutes(30),
regenerateIdentity:(管理器,用户)=>user.GenerateUserIdentityAsync(管理器))
}
});            
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}

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

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

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

在启动中设置断点可能很困难,因此请尝试类似的操作,以查看启动被初始化的确切次数

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

显示相关代码我添加了代码狙击手。谢谢你有没有想过为什么要叫两次?我也有同样的问题。