我们有 2 个应用程序都使用 Asp.Net Identity 来确保安全。
它们彼此无关,但我恰好是这两个项目的开发人员。
我在 Cookie 名称方面遇到了一个非常烦人的问题。如果我转到 app1 并登录,然后转到 app2 并登录,我会与 app1 断开连接。
我的大胆猜测是,这是因为这两个应用程序共享相同的 cookie 名称。
因此,为了便于开发,也因为我认为这样更好,我正在寻找一种更改 cookie 名称的方法。
有什么线索吗?
最佳答案
好的,找到了。
默认情况下,VS 和 Identity 会在 App_Start 中创建一个名为 Startup.Auth.cs 的文件。
该文件包含以下代码
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
要解决我们的问题,我们必须设置 CookieAuthenticationOptions
的 CookieName 属性
CookieName = "my-very-own-cookie-name"
就是这样;仅此而已。
干杯!
关于Asp.Net 身份和 cookie 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25038303/