【问题标题】:Manually Authenticate a User with Claims for every request为每个请求手动验证用户的声明
【发布时间】:2019-07-08 18:54:25
【问题描述】:

我在 API 上使用 ASP.NET Core 2.2 和 ASP.NET Identity。

是否可以为每个请求手动验证用户的声明?

我想在初始开发阶段使用它...

【问题讨论】:

    标签: asp.net-core asp.net-identity asp.net-core-2.1 asp.net-identity-3 asp.net-core-2.2


    【解决方案1】:

    要在不登录的情况下验证用户身份,您可以尝试:

    public async Task<IActionResult> Login()
    {
        var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
        identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "edward"));
        identity.AddClaim(new Claim(ClaimTypes.Name, "edward zhou"));
        //add your own claims 
        var principal = new ClaimsPrincipal(identity);
        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties { IsPersistent = true });            
        return View();
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-16
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 2023-04-07
      • 2011-01-28
      • 2021-09-17
      相关资源
      最近更新 更多