为每个请求手动验证具有声明的用户

问题描述 投票:-1回答:1

我在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个回答
0
投票

要在没有记录的情况下对用户进行身份验证,您可以尝试:

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();
}
© www.soinside.com 2019 - 2024. All rights reserved.