ASP.NET核心身份和饼干
我有一个ASP.NET核心网站使用AspNetCore.Identity.EntityFrameworkCore 1.1.1和cookie来授权/认证我的用户。不管我在下面的代码中选择什么作为我的设置,cookie在大约20分钟后过期,我无法弄清楚为什么。除非您关闭浏览器并清除历史记录/ cookies,否则该网站将不再起作用。有任何想法吗?ASP.NET核心身份和饼干
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>
}
当您使用ASPNET身份你并不需要一个单独的CookieAuthentication中间件。 UseIdentity()
将为你做这件事,并生成一个cookie。您可以设置"cookie options"
在应用程序的AddIdentity块,像这样:
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://*.com/a/34981457/1137785,它给这种情景的一个很好的解释背景。
谢谢@Muqeet。您的回复帮助我解决了我的问题! –
@muqeetKhan在[this](https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/identity?tabs=visual-studio%2Caspnetcore1x#tabpanel_yS0QDAFpXJ-1_aspnetcore1x)新的ASP.NET文章中正在使用以下设置选项。你的方法和文章的方法有什么不同,你知道吗? '//配置身份 services.Configure
@nam只是不同的扩展方法。在我的示例中,我使用的扩展方法采用'options'对象然后在该方法中的DI中配置,在你给的链接中,他们只是单独做它的开放源代码yay!在[github](https://github.com/aspnet/Identity/blob/patch /1.1.4/src/Microsoft.AspNetCore.Identity/IdentityServiceCollectionExtensions.cs) –
我认为问题在于我将数据持久化为具有不同设置的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);
你也可以发布你的AddIdentity区块吗? –
我已经使用ConfigureServices的AddIdentity块更新了我的问题。 –