asp.net-core - SignInWithClaimsAsync 中的声明永久到期
问题描述
我正在使用带有 Identity 的 ASP.NET Core 3.1,并使用以下代码在声明中存储一些基本用户信息,例如他们的全名(我知道检查密码和其他内容,为简洁起见忽略它):
var user = await _userManager.FindByNameAsync(Input.Username);
var claims = new List<Claim>
{
new Claim("UserFullname", user.Fullname, ClaimValueTypes.String)
}
await _signInManager.SignInWithClaimsAsync(user, Input.RememberMe, claims);
我正在_Layout.cshtml
使用以下行访问它:
var userFullname = User.Claims.Single(c => c.Type == "UserFullname").Value;
问题是,即使用户仍然登录,这似乎会在一段时间内过期。我希望这在用户注销之前是永久的。
我确信必须有某种方法startup.cs
来控制这一点,并且尽可能避免覆盖任何东西。
--EDIT--
正如@yinqiu 回答的评论中提到的,我使用以下行尝试了cookie身份验证方案:
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme);
但这也无济于事。
解决方案
我认为您可以尝试覆盖该SignInWithClaimsAsync
方法。
public override async Task SignInWithClaimsAsync(ApplicationUser user, AuthenticationProperties authenticationProperties, System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> additionalClaims)
{
if (authenticationProperties != null && authenticationProperties.IsPersistent)
{
authenticationProperties.ExpiresUtc = DateTimeOffset.UtcNow.AddYears(1);
}
await base.SignInWithClaimsAsync(user, authenticationProperties, additionalClaims);
}
推荐阅读
- c# - 动态更改datagrid行单元格前景色wpf
- flutter - 为什么它给出无效的常量值?
- android - 尝试在空对象引用上调用虚拟方法 '.MutableLiveData .getLiveData(java.lang.String)'
- mysql - 试图在子查询中获取行数
- swift - Swift - 如何从视图之外的 func 访问@Published var?
- css - 覆盖表边界的助推器 4
- android - 带有滚动的列表视图将页脚 Xamarin android 推出 UI
- sql - 为 Hacker Rank Challenge 编写的 SQL 查询优化
- arrays - 如何使用数组公式生成工资单条件
- pandas - 使用从 1 df 到另一个 pandas 的列值