ASP .NET CORE 2.2 JWT & Claims 网站身份验证
ASP .NET CORE 2.2 JWT & Claims identity Authentication for Website
我有一个 .net core 2.2 api,它生成(在成功登录时)一个 JWT 令牌,其中包含一个声明身份,该身份传递经过身份验证的用户的用户名、权限和角色等信息。
在我的 .net 核心 2.2 中。 Web 应用程序我有一个登录机制,它通过控制器的用户检索 JWT 令牌。
我的问题是
如何从我的登录控制器中扩展令牌并设置我的网络应用程序以包括使用 User.Identity.IsAuthenticated
、User.IsInRole("Admin")
等身份验证机制和 [=13= 等控制器操作] 和 [Authorize(Roles="Admin")]
有人指示我查看 facebook/google 等外部身份验证提供程序背后的源代码,但无济于事。
提前致谢。
第一步是在 Startup.cs
中使用 cookie authentication
:
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
在Configure
方法中,使用UseAuthentication
方法调用设置HttpContext.User属性的认证中间件。在调用 UseMvcWithDefaultRoute
或 UseMvc
:
之前调用 UseAuthentication 方法
app.UseAuthentication();
然后在您的身份验证控制器中,在获取令牌并解码以获取声明后,您应该创建新的 ClaimsIdentity
,添加您的声明和登录用户:
if (!User.Identity.IsAuthenticated)
{
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, YourName));
identity.AddClaim(new Claim(ClaimTypes.Name, YourName));
identity.AddClaim(new Claim(ClaimTypes.Role, "Admin"));
//Add your custom claims
var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties { IsPersistent = true });
}
之后,您可以使用User.Identity.IsAuthenticated
、User.IsInRole("Admin")
和[Authorize(Roles="Admin")]
:
[Authorize(Roles = "Admin")]
public IActionResult About()
{
var result = User.IsInRole("Admin");
return View();
}
我有一个 .net core 2.2 api,它生成(在成功登录时)一个 JWT 令牌,其中包含一个声明身份,该身份传递经过身份验证的用户的用户名、权限和角色等信息。
在我的 .net 核心 2.2 中。 Web 应用程序我有一个登录机制,它通过控制器的用户检索 JWT 令牌。
我的问题是
如何从我的登录控制器中扩展令牌并设置我的网络应用程序以包括使用 User.Identity.IsAuthenticated
、User.IsInRole("Admin")
等身份验证机制和 [=13= 等控制器操作] 和 [Authorize(Roles="Admin")]
有人指示我查看 facebook/google 等外部身份验证提供程序背后的源代码,但无济于事。
提前致谢。
第一步是在 Startup.cs
中使用 cookie authentication
:
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
在Configure
方法中,使用UseAuthentication
方法调用设置HttpContext.User属性的认证中间件。在调用 UseMvcWithDefaultRoute
或 UseMvc
:
app.UseAuthentication();
然后在您的身份验证控制器中,在获取令牌并解码以获取声明后,您应该创建新的 ClaimsIdentity
,添加您的声明和登录用户:
if (!User.Identity.IsAuthenticated)
{
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, YourName));
identity.AddClaim(new Claim(ClaimTypes.Name, YourName));
identity.AddClaim(new Claim(ClaimTypes.Role, "Admin"));
//Add your custom claims
var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties { IsPersistent = true });
}
之后,您可以使用User.Identity.IsAuthenticated
、User.IsInRole("Admin")
和[Authorize(Roles="Admin")]
:
[Authorize(Roles = "Admin")]
public IActionResult About()
{
var result = User.IsInRole("Admin");
return View();
}