【问题标题】:How to assign a user manually如何手动分配用户
【发布时间】:2021-03-15 06:31:44
【问题描述】:

我拥有所有声明,并且我还检查了令牌是否有效。我如何 signin 这个新用户 IsAuthenticated=true 和声明主体

            var tokenHandler = new JwtSecurityTokenHandler();
            var validationParameters = new TokenValidationParameters()
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(conf.GetSection("JWTKey").Value)),
                ValidateAudience = false,
                ValidateIssuer = false
            }
            ;

            SecurityToken validatedToken;
            IPrincipal pr= tokenHandler.ValidateToken(token, validationParameters, out validatedToken);

            var claims = tokenHandler.ReadJwtToken(token).Claims;
            var claimspr = new ClaimsPrincipal(new ClaimsIdentity(cliams,"JWTAuth"));

【问题讨论】:

  • AuthenticationManager 没有signin 方法
  • 你的AuthenticationManager是这样实现的吗?私人 IAuthenticationManager AuthenticationManager { get { return HttpContext.GetOwinContext().Authentication; } }
  • 哦,我使用的是“AuthenticationManager”类而不是接口。有没有其他方法可以做到这一点? “IAuthenticationManager”对我大喊大叫,说它与我的应用程序不兼容。尝试安装旧版本也没有用。

标签: asp.net-core .net-core asp.net-core-mvc


【解决方案1】:

当您使用 .net 核心时。你可以使用HttpContext.SignInAsync并传递ClaimsPrincipal作为参数。见this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多