authentication - 调用 HttpContext.SignInAsync 后 User.Identity.IsAuthenticated 为 false
问题描述
我正在使用 asp.net 核心并编写自定义登录。
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, "SomeUserName")
// ... other stuffs
};
var claimsIdentity = new ClaimsIdentity(
claims, CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties();
var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
claimsPrincipal,
authProperties);
此时一切正常,并且在浏览器中设置了 auth-cookie,但是下次我刷新页面时,在任何 controller.action 方法中,User.Identity.IsAhthenticated
isfalse
和User.Identity.Name
null。
这也是我的startup.cs
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.Cookie.SameSite = SameSiteMode.None;
options.EventsType = typeof(CustomCookieAuthenticationEvents);
});
// ...
app.UseAuthentication();
我怎样才能得到User.Identity.IsAhthenticated
真实?
解决方案
推荐阅读
- javascript - CSS transform-origin 不适用于 svg 在 safari
- jmx - 使用 JDK11 在 Wildfly 15 域配置中设置 JMX 导出
- javascript - 如何将数组中的对象的值添加到另一个数组中的另一个对象中?
- c - Realloc 现有数据正在丢失
- python - 如何在整个熊猫列上应用 json_normalize
- ruby-on-rails - 活动存储 - 带有 URL 或路径的原始文件扩展名
- c++ - 我如何从 C++ 中操作 csv
- i2c - 与 Wire.h(ESP32) 的 I2C 通信
- python-3.x - 我应该使用循环还是坚持使用嵌套的 if 语句?
- python - 如何使用数据框拆分新列中的两个 CSV 文件列,在熊猫中显示匹配项?