使用Owin SessionStore进行cookie存储仍然会将cookie发送到浏览器

时间:2018-08-17 22:40:04

标签: c# webforms owin stackexchange.redis

我已经使用Redis实现了cookie存储。我大部分时间都遵循this代码示例...

我的Startup.Auth类没什么特别的:

public void ConfigureAuth(IAppBuilder app)
{
    app.CreatePerOwinContext(ApplicationDbContext.Create);
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
    app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

    IDataProtector dataProtector = app.CreateDataProtector(typeof(RedisAuthenticationTicket).FullName);

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,

        LoginPath = new PathString("/Login.aspx"),
        LogoutPath = new PathString("/Register.aspx"),
        SessionStore = new RedisSessionStore(new TicketDataFormat(dataProtector)),

        Provider = new CookieAuthenticationProvider
        { 
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                validateInterval: TimeSpan.FromSeconds(300),
                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)),

        }
    });
...

我的印象是,实施SessionStore意味着我在浏览器中看到的唯一会话cookie是ASPNET_SessionId,但是我的应用似乎在登录后仍会创建一个AspNetCookie ...这肯定是来自Owin初创公司-我可以在启动时更改它的名称。

enter image description here

我的AuthenticationSessionStore类正在触发并从Redis获取身份验证cookie。我只是不知道为什么这个Cookie仍然存在,以及它是否是故意存在的?

1 个答案:

答案 0 :(得分:1)

按照下面的评论进行编辑

.AspNet.Cookies是身份验证cookie,它使服务器可以知道用户何时登录到您的应用程序。

您使用Redis的事实并不意味着不再需要Cookie。 如果用户A和B没有发送Cookie,您如何区分他们?

如果您不使用Redis,则身份验证cookie值将包含用户信息。 使用Redis时,用户信息存储在此,并且身份验证cookie值包含允许访问此信息的Redis密钥。

会话cookie与征服cookie的不同之处在于,无需用户登录即可为您存储用户的会话数据。假设这对于一个电子商务应用程序很有用,您可以在该应用程序中向未登录和已登录的用户显示他们查看过哪些产品。

相同的概念可能适用于会话:您可以决定使用外部存储,而不是将数据存储在cookie中,从而使在服务器与浏览器之间传播的数据量最小(仅会话密钥)


原始答案

仍然需要一个cookie来识别用户。

区别在于cookie现在不再包含会话数据,而仅包含Redis密钥,该密钥允许访问会话数据。

相关问题