【问题标题】:ASP.NET Core Identity & CookiesASP.NET Core 身份和 Cookie
【发布时间】:2017-06-12 13:18:58
【问题描述】:

我有一个 ASP.NET Core 站点,它使用 AspNetCore.Identity.EntityFrameworkCore 1.1.1 和 cookie 来授权/验证我的用户。无论我在下面的代码中选择什么设置,cookie 都会在大约 20 分钟后过期,我不知道为什么。除非您关闭浏览器并清除历史记录/cookie,否则该网站将不再工作。有什么想法吗?

services.AddIdentity<ApplicationUser, IdentityRole>(config =>
{
    //  Require a confirmed email in order to log in
    config.SignIn.RequireConfirmedEmail = true;
})
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

app.UseIdentity();

//  Add cookie middleware to the configure an identity request and persist it to a cookie.
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationScheme = "Cookie",
    LoginPath = new PathString("/Account/Login/"),
    AccessDeniedPath = new PathString("/Account/Forbidden/"),
    AutomaticAuthenticate = true,
    AutomaticChallenge = true,
    ExpireTimeSpan = TimeSpan.FromMinutes(20),
    SlidingExpiration = true,

});

我还有一些剃须刀代码控制是否在 _layout 页面上显示管理菜单。当 cookie 过期时,这会崩溃,因为用户突然没有声明。有没有更好的方法来处理这个问题?

// If user is admin then show drop down with admin navigation
@if (User.HasClaim(System.Security.Claims.ClaimTypes.Role, "admin"))
{
    <ul class="nav navbar-nav">
        @*etc*@
    </ul>
}

【问题讨论】:

  • 您能否也发布您的 AddIdentity 部分块?
  • 我已经用 ConfigureServices 的 AddIdentity 块更新了我的问题。

标签: session cookies asp.net-core asp.net-core-identity


【解决方案1】:

当您使用 ASPNET 身份时,您不需要单独的 CookieAuthentication 中间件。 UseIdentity() 将为您执行此操作并生成一个 cookie。您可以在应用程序的 AddIdentity 块中设置"cookie options",如下所示:

     services.AddIdentity<ApplicationUser, IdentityRole>(config =>
            {
                //  Require a confirmed email in order to log in
                config.SignIn.RequireConfirmedEmail = true;

               // Your Cookie settings
              config.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromDays(1);
              config.Cookies.ApplicationCookie.LoginPath = "/Account/LogIn";
              config.Cookies.ApplicationCookie.LogoutPath = "/Account/LogOut";
            }).AddEntityFrameworkStores<ApplicationDbContext().AddDefaultTokenProviders();

另外,看看https://stackoverflow.com/a/34981457/1137785,它给出了这种场景的背景,并有很好的解释。

【讨论】:

  • 谢谢@Muqeet。您的回复帮助我解决了我的问题!
  • @muqeetKhan 在this 新的 ASP.NET 文章中,他们使用以下设置选项。你的方法和文章的方法有什么区别,你知道吗? // Configure Identity services.Configure&lt;IdentityOptions&gt;(options =&gt; {.....}
  • @nam 只是不同的扩展方法。在我的示例中,我使用了扩展方法,该方法采用 options 对象,然后在方法中的 DI 中配置该对象。在您提供的链接中,他们只是单独进行。它的开源耶!看这里github
  • 要么我是盲人,要么在 3.1 中 config.Cookies 消失了。我现在应该如何配置LoginPath
  • 已移至 services.ConfigureApplicationCookie(configure => { ... });
【解决方案2】:

我认为问题在于我将数据保存到具有不同设置的 cookie 中。

不确定这是否是正确的方法,但我能够通过同时使用 services.AddIdentity 和 app.UseCookieAuthentication 来解决问题,如下所示。

在ConfigureServices中,设置登录cookie:

        //  set the cookie for sign in
        services.AddIdentity<ApplicationUser, IdentityRole>(config =>
        {               
            //  Require a confirmed email in order to log in
            config.SignIn.RequireConfirmedEmail = true;
            // Cookie settings
            config.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromHours(10);
            config.Cookies.ApplicationCookie.LoginPath = "/Account/LogIn";
            config.Cookies.ApplicationCookie.LogoutPath = "/Account/LogOut";
        }).AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();

在配置中设置用于持久声明的 cookie 方案:

        //  Add cookie middleware to the configure an identity request and persist it to a cookie.
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationScheme = "Cookie",
            LoginPath = new PathString("/Account/Login/"),
            AccessDeniedPath = new PathString("/Account/Forbidden/"),
            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
            //ExpireTimeSpan = TimeSpan.FromSeconds(10),
            ExpireTimeSpan = TimeSpan.FromHours(10),
            SlidingExpiration = true,
        });

在登录方法中,持久化声明:

await HttpContext.Authentication.SignInAsync("Cookie", userPrincipal);

【讨论】:

    猜你喜欢
    • 2017-03-30
    • 2018-09-02
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 2017-02-15
    • 1970-01-01
    相关资源
    最近更新 更多