无法执行使用 cookie 身份验证登录
Can not perform sign in with cookie authentication
我正在尝试登录,但 HttpContext.User.Identitiy.IsAuthenticated
总是错误的。
配置服务
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(
options => {
options.LoginPath = "/";
options.AccessDeniedPath =new PathString("/AccessDenied");
options.Events.OnRedirectToLogin = (context) => {
context.Response.StatusCode = 401;
return Task.CompletedTask;
};
});
方法
public async Task Invoke(HttpContext context) {
string token = context.Request.Query["token"];
var claims = new List<Claim> {
new Claim("token",token,APPLICATION_NAME)
};
var claimsIdentity = new ClaimsIdentity(claims,CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties {
AllowRefresh = true,
ExpiresUtc = DateTimeOffset.Now.AddSeconds(20),
IsPersistent = true
};
await context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity),authProperties);
if (context.User.Identity.IsAuthenticated) { //always false
}
}
SignInAsync
不会更改 当前请求的 用户主体。在下一个请求中检查相同的 属性,它应该是 true
我正在尝试登录,但 HttpContext.User.Identitiy.IsAuthenticated
总是错误的。
配置服务
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(
options => {
options.LoginPath = "/";
options.AccessDeniedPath =new PathString("/AccessDenied");
options.Events.OnRedirectToLogin = (context) => {
context.Response.StatusCode = 401;
return Task.CompletedTask;
};
});
方法
public async Task Invoke(HttpContext context) {
string token = context.Request.Query["token"];
var claims = new List<Claim> {
new Claim("token",token,APPLICATION_NAME)
};
var claimsIdentity = new ClaimsIdentity(claims,CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties {
AllowRefresh = true,
ExpiresUtc = DateTimeOffset.Now.AddSeconds(20),
IsPersistent = true
};
await context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity),authProperties);
if (context.User.Identity.IsAuthenticated) { //always false
}
}
SignInAsync
不会更改 当前请求的 用户主体。在下一个请求中检查相同的 属性,它应该是 true