C# Asp.net core 1.1验证安全戳isn';行不通
好吧,我已经为此挣扎了两天了,但找不到任何相关信息 我使用的是asp.NETCore1.1,我已经在我的系统中实现了身份验证,它工作得很好。但我的问题是,用户在更新安全标记后仍保持在线 我尝试将C# Asp.net core 1.1验证安全戳isn';行不通,c#,asp.net-core,asp.net-core-mvc,asp.net-core-1.1,asp.net-core-identity,C#,Asp.net Core,Asp.net Core Mvc,Asp.net Core 1.1,Asp.net Core Identity,好吧,我已经为此挣扎了两天了,但找不到任何相关信息 我使用的是asp.NETCore1.1,我已经在我的系统中实现了身份验证,它工作得很好。但我的问题是,用户在更新安全标记后仍保持在线 我尝试将SecurityStampValidationInterval设置为0,1秒,但似乎没有任何效果 在这一点上,我不确定它是否是一个bug?还是我做错了什么 以下是我的标识选项的外观: services.Configure<IdentityOptions>(options =>
SecurityStampValidationInterval
设置为0,1
秒,但似乎没有任何效果
在这一点上,我不确定它是否是一个bug?还是我做错了什么
以下是我的标识选项的外观:
services.Configure<IdentityOptions>(options =>
{
options.Tokens.PasswordResetTokenProvider = _defaultTokenProviderName;
options.Password.RequireDigit = true;
options.Password.RequiredLength = 6;
options.Password.RequireLowercase = true;
options.Password.RequireUppercase = true;
options.Password.RequireNonAlphanumeric = false;
options.User.RequireUniqueEmail = true;
options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.";
options.SecurityStampValidationInterval = TimeSpan.Zero;
options.Cookies.ApplicationCookie.CookieName = "Identity_cookie";
options.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromHours(1);
options.Cookies.ApplicationCookie.SlidingExpiration = true;
options.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents()
{
OnRedirectToLogin = (context) =>
{
var cul = context.HttpContext.Features.Get<IRequestCultureFeature>();
var lang = cul.RequestCulture.Culture.TwoLetterISOLanguageName;
context.Response.Redirect($"/{lang}/Account/Login?{context.Options.ReturnUrlParameter}" +
$"={System.Net.WebUtility.UrlEncode(context.Request.Path + context.Request.QueryString)}");
return Task.FromResult(0);
},
};
});
提前谢谢。我更新了我的问题以包含代码的其余部分(很抱歉,它在服务下。AddIdentity..
下,所以我忘了复制它)
因此,我的问题是设置options.Cookies.ApplicationCookie.Events
以便从标识选项使用自定义OnRedirectToLogin
,出于某种原因,我不得不将其移动到app.useCookie身份验证()
我的新代码是:
app.UseIdentity();
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
Events = new CookieAuthenticationEvents()
{
OnRedirectToLogin = (context) =>
{
var cul = context.HttpContext.Features.Get<IRequestCultureFeature>();
var lang = cul.RequestCulture.Culture.TwoLetterISOLanguageName;
context.Response.Redirect($"/{lang}/Account/Login?{context.Options.ReturnUrlParameter}" +
$"={System.Net.WebUtility.UrlEncode(context.Request.Path + context.Request.QueryString)}");
return Task.FromResult(0);
},
}
});
我不知道这是一个bug还是什么,但是配置应该在ConfigureServices
中,对吗?您似乎已经扩展了ApplicationUser以添加Blocked属性?为什么不设置LockoutEnd,它应该达到相同的结果?另外,我建议您查看UpdateSecurityStampAsync和UpdateAsync返回的IdentityResult,以确保成功。是的,我正在扩展IdentityUser
以添加一些属性,如DisplayName
Blocked
ProfileImage
。使用lockoutEnd作为阻止因素的问题是,我们可能会在30分钟左右尝试10次错误后使用它锁定帐户。因此,基于锁定状态阻止用户的帖子是不好的,但我将尝试一下,看看它是否对验证SecurityStamp
有任何影响。关于检查IdentityResult
两者都成功,数据库值随SecurityStamp
一起更改,我甚至手动更改了它,以查看它是否对登录用户有任何影响,但没有任何影响。我还尝试删除/更改expireTimeSpan
以及slidimgexpatition
以检查它是否与安全戳验证器接口,但没有成功。可能会添加options.onsecuritySampRefreshingPrincipal=c=>{return Task.FromResult(0);}代码>到services.AddIdentity并设置一个断点以查看它是否被命中?尝试过了,根据我的记忆,断点从未被命中。使用或不使用expireTimeSpan、SlidingExpirement
进行了尝试。我今天可能会尝试将Microsoft.Aspnetcore.Authentication更新为2 preview
版本,看看它是否有任何区别。您可能应该标记此w/asp.net-core-identity或在上创建一个问题,以获得更好的响应。
app.UseIdentity();
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
Events = new CookieAuthenticationEvents()
{
OnRedirectToLogin = (context) =>
{
var cul = context.HttpContext.Features.Get<IRequestCultureFeature>();
var lang = cul.RequestCulture.Culture.TwoLetterISOLanguageName;
context.Response.Redirect($"/{lang}/Account/Login?{context.Options.ReturnUrlParameter}" +
$"={System.Net.WebUtility.UrlEncode(context.Request.Path + context.Request.QueryString)}");
return Task.FromResult(0);
},
}
});
options.Tokens.PasswordResetTokenProvider = _defaultTokenProviderName;
options.Password.RequireDigit = true;
options.Password.RequiredLength = 6;
options.Password.RequireLowercase = true;
options.Password.RequireUppercase = true;
options.Password.RequireNonAlphanumeric = false;
options.User.RequireUniqueEmail = true;
options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.";
options.SecurityStampValidationInterval = TimeSpan.Zero;
options.Cookies.ApplicationCookie.CookieName = "Identity_cookie";
options.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromHours(1);
options.Cookies.ApplicationCookie.SlidingExpiration = true;