AuthenticationStateProvider中的UserID为空
创始人
2024-09-22 14:31:10
0
  1. 确认使用的认证方案是否正确,在 Startup.cs 文件中进行配置:
services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = "Identity.Application";
    options.DefaultChallengeScheme = "Identity.Application";
}).AddCookie("Identity.Application");
  1. 在认证成功后更新 AuthenticationStateProvider 中的 UserID,在 Login() 方法中添加以下代码:
var claims = new List
{
    new Claim(ClaimTypes.NameIdentifier, user.Id)
};
var userIdentity = new ClaimsIdentity(claims, "login");
var userPrincipal = new ClaimsPrincipal(userIdentity);

await _httpContextAccessor.HttpContext.SignInAsync(
    CookieAuthenticationDefaults.AuthenticationScheme,
    userPrincipal);
  1. 确认 AuthenticationStateProvider 是否正确实现,例如:
public class CustomAuthenticationStateProvider : AuthenticationStateProvider
{
    public override Task GetAuthenticationStateAsync()
    {
        // 获取当前用户的 UserID
        var userId = _httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);

        // 如果 UserID 存在,返回已认证的状态
        if (!string.IsNullOrEmpty(userId))
        {
            var claims = new List
            {
                new Claim(ClaimTypes.NameIdentifier, userId)
            };
            var identity = new ClaimsIdentity(claims, "login");
            var principal = new ClaimsPrincipal(identity);
            return Task.FromResult(new AuthenticationState(principal));
        }

        // 如果 UserID 为空,返回未认证的状态
        return Task.FromResult(new AuthenticationState(new ClaimsPrincipal()));
    }
}

相关内容

热门资讯

2026版总结!hhpoker... 2026版总结!hhpoker脚本下载,德普之星辅助工具如何打开,切实真的是有挂(有挂教程)1、该软...
推出新举措!悦扑克脚本,德普之... 推出新举措!悦扑克脚本,德普之星透视辅助,果然确实有挂(有挂秘诀)亲,关键说明,德普之星透视辅助透视...
今天上午!wepoker辅助器... 今天上午!wepoker辅助器软件下载,德普之星私人局透视,都是是有挂(有挂实锤)1)德普之星私人局...
攻略辅助挂!wpk有辅助器吗,... 攻略辅助挂!wpk有辅助器吗,德普之星透视辅助软件是真的吗,本来存在有挂(新版有挂)亲,关键说明,德...
据目击者称!xpoker辅助助... 据目击者称!xpoker辅助助手,德普之星透视辅助软件下载,一直真的有挂(有挂详情)进入游戏-大厅左...
刚刚!智星菠萝透视,智星德州辅... 刚刚!智星菠萝透视,智星德州辅助译码插件靠谱吗,好像是有挂(的确有挂)进入游戏-大厅左侧-新手福利-...
教学辅助挂!wepokerh5... 教学辅助挂!wepokerh5破解,德普之星app安卓版破解版,都是确实有挂(有挂详情)1、许多玩家...
此事引发网友热议!pokemm... 此事引发网友热议!pokemmo手机版脚本,德普之星透视辅助插件,总是确实有挂(有挂方针)1、完成德...
针对!wepoker怎么挂飞机... 针对!wepoker怎么挂飞机,德扑之星私人局透视,好像是有挂(有挂教学)1、下载好德扑之星私人局透...
据权威媒体报道!aapoker... 据权威媒体报道!aapoker发牌逻辑,德普之星有透视辅助吗,总是真的有挂(详细教程)1、德普之星有...