Asp.Net 身份和 cookie 名称

标签 asp.net cookies asp.net-identity

我们有 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/

相关文章:

asp.net - 允许用户更改其帐户页面配色方案的方法

asp.net - 从 JSON 对象中提取字符串值

c# - 如何动态设置 IFrame 的源?

java - 尝试使用 HttpClient 设置 session cookie

c# - ASP.NET Identity 使用 ConfigureAwait(false),为什么安全?

asp.net - 如何编辑 web.config 以停止为 .NET Web 应用程序的特定文件类型提供服务

c# - 如何使用 JavaScript 在 MVC3 中的 View 之间保留数据?

javascript - slideToggle 与 $.cookie

asp.net-core - 如何将dapper与ASP.Net核心身份一起使用?

asp.net - UserManager.GenerateEmailConfirmationToken 返回不适合 URL 传输的 token