以这种方式配置后无法正常工作。
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)
我希望两者具有相同的行为。显然不是。错误或我没有如何配置它? :-)
任何想法。
答案 0 :(得分:0)
在发布我的问题时,我也配置/添加了身份框架。因此,可能是由于多种因素共同作用,它可能无法正常工作。
有效的解决方案:
配置:
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的所有用法。