gpt4 book ai didi

asp.net - 为什么会在页面加载前后调用Owin Startup类的Configuration方法?

转载 作者:行者123 更新时间:2023-12-01 06:24:10 25 4
gpt4 key购买 nike

OWIN 启动类的Configuration 方法在asp.net 身份应用程序中被调用两次。该方法被调用两次,即在服务器端页面加载之前和之后。
我已经找到了一个与之相关的问题( Why is the Configuration method of the SignalR Startup class invoked twice ),但它并没有让我明白什么是代码的预初始化和后初始化。为什么需要它,它会在我的应用程序中出现任何性能问题吗?

这是运行两次的代码:

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

最佳答案

我不相信它在页面之前和之后加载,但我有类似的情况,我有一个虚拟目录 /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 });
}
}

关于asp.net - 为什么会在页面加载前后调用Owin Startup类的Configuration方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30433940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com