【发布时间】: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
我在 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
要在不登录的情况下验证用户身份,您可以尝试:
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();
}
【讨论】: