这样
配置它不起作用。
services
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
options.LoginPath = $"/logon";
options.LogoutPath = $"/logoff";
options.AccessDeniedPath = $"/accessdenied";
options.SlidingExpiration = true;
})
以这种方式配置它正在工作:
services.ConfigureApplicationCookie(options =>
{
options.Cookie.Name = "Caldr.Auth";
options.LoginPath = $"/logon";
options.LogoutPath = $"/logoff";
options.AccessDeniedPath = $"/accessdenied";
});
services
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
我希望两者都有相同的行为。显然不是。错误或我没有如何配置它?:-)
任何想法。
在发布我的问题时,我也配置/添加了身份框架。因此,可能是几个因素的混合,可能无法正常工作。
工作解决方案:
配置:
var authenticationBuilder = services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = $"/logon";
options.LogoutPath = $"/logoff";
options.AccessDeniedPath = $"/accessdenied";
});
ConfigureSocialLogins(authenticationBuilder);
实际登录(即通过 Cookie 编写(
private async Task SignInUser(AppUser appUser)
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, appUser.Email),
new Claim(ClaimTypes.Name, appUser.Displayname ?? appUser.Email),
new Claim(ClaimTypes.Email, appUser.Email),
};
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties());
}
记下 CookieAuthenticationDefaults.AuthenticationScheme 的所有用法。