无法使用 ASP.NET Core 从 JWT 令牌中获取声明
Can't get claims from JWT token with ASP.NET Core
我正在尝试使用 ASP.NET 核心实现 JWT 持有者身份验证的非常简单的实现。我 return 来自控制器的响应有点像这样:
var identity = new ClaimsIdentity();
identity.AddClaim(new Claim(ClaimTypes.Name, applicationUser.UserName));
var jwt = new JwtSecurityToken(
_jwtOptions.Issuer,
_jwtOptions.Audience,
identity.Claims,
_jwtOptions.NotBefore,
_jwtOptions.Expiration,
_jwtOptions.SigningCredentials);
var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);
return new JObject(
new JProperty("access_token", encodedJwt),
new JProperty("token_type", "bearer"),
new JProperty("expires_in", (int)_jwtOptions.ValidFor.TotalSeconds),
new JProperty(".issued", DateTimeOffset.UtcNow.ToString())
);
我有传入请求的 Jwt 中间件:
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
TokenValidationParameters = tokenValidationParameters
});
这似乎可以保护具有授权属性的资源,但声明从未出现。
[Authorize]
public async Task<IActionResult> Get()
{
var user = ClaimsPrincipal.Current.Claims; // Nothing here
您不能在 ASP.NET 核心应用程序中使用 ClaimsPricipal.Current
,因为它不是由运行时设置的。您可以阅读 https://github.com/aspnet/Security/issues/322 了解更多信息。
相反,请考虑使用 User
属性,由 ControllerBase
公开。
访问 User.Claims
而不是 ClaimsPrinciple.Current.Claims
。
From Introduction to Identity at docs.asp.net:
...inside the HomeController.Index
action method, you can view the User.Claims
details.
Here is the relevant source code 来自 MVC 存储库:
public ClaimsPrincipal User
{
get
{
return HttpContext?.User;
}
}
作为 ASP.NET Core 2.0 的一部分,您可以阅读上述 Shaun 的 JWT 声明。如果您只是在寻找用户 ID(确保您已经使用 "Sub" 声明名称将其添加为声明的一部分),那么您可以根据您的用例使用以下两个示例来阅读:
读取用户 ID 声明:
public class AccountController : Controller
{
[Authorize]
[HttpGet]
public async Task<IActionResult> MethodName()
{
var userId = _userManager.GetUserId(HttpContext.User);
//...
return Ok();
}
}
阅读其他声明:
public class AccountController : Controller
{
[Authorize]
[HttpGet]
public async Task<IActionResult> MethodName()
{
var rolesClaim = HttpContext.User.Claims.Where( c => c.Type == ClaimsIdentity.DefaultRoleClaimType).FirstOrDefault();
//...
return Ok();
}
}
使用此解决方案,您可以在使用 JWT 令牌时访问 User.Identity
及其在控制器中的声明:
第 1 步:创建 JwtTokenMiddleware:
public static class JwtTokenMiddleware
{
public static IApplicationBuilder UseJwtTokenMiddleware(
this IApplicationBuilder app,
string schema = "Bearer")
{
return app.Use((async (ctx, next) =>
{
IIdentity identity = ctx.User.Identity;
if (identity != null && !identity.IsAuthenticated)
{
AuthenticateResult authenticateResult = await ctx.AuthenticateAsync(schema);
if (authenticateResult.Succeeded && authenticateResult.Principal != null)
ctx.User = authenticateResult.Principal;
}
await next();
}));
}
}
第 2 步:在 Startup.cs 中使用它:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAuthentication();
app.UseJwtTokenMiddleware();
}
我正在尝试使用 ASP.NET 核心实现 JWT 持有者身份验证的非常简单的实现。我 return 来自控制器的响应有点像这样:
var identity = new ClaimsIdentity();
identity.AddClaim(new Claim(ClaimTypes.Name, applicationUser.UserName));
var jwt = new JwtSecurityToken(
_jwtOptions.Issuer,
_jwtOptions.Audience,
identity.Claims,
_jwtOptions.NotBefore,
_jwtOptions.Expiration,
_jwtOptions.SigningCredentials);
var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);
return new JObject(
new JProperty("access_token", encodedJwt),
new JProperty("token_type", "bearer"),
new JProperty("expires_in", (int)_jwtOptions.ValidFor.TotalSeconds),
new JProperty(".issued", DateTimeOffset.UtcNow.ToString())
);
我有传入请求的 Jwt 中间件:
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
TokenValidationParameters = tokenValidationParameters
});
这似乎可以保护具有授权属性的资源,但声明从未出现。
[Authorize]
public async Task<IActionResult> Get()
{
var user = ClaimsPrincipal.Current.Claims; // Nothing here
您不能在 ASP.NET 核心应用程序中使用 ClaimsPricipal.Current
,因为它不是由运行时设置的。您可以阅读 https://github.com/aspnet/Security/issues/322 了解更多信息。
相反,请考虑使用 User
属性,由 ControllerBase
公开。
访问 User.Claims
而不是 ClaimsPrinciple.Current.Claims
。
From Introduction to Identity at docs.asp.net:
...inside the
HomeController.Index
action method, you can view theUser.Claims
details.
Here is the relevant source code 来自 MVC 存储库:
public ClaimsPrincipal User
{
get
{
return HttpContext?.User;
}
}
作为 ASP.NET Core 2.0 的一部分,您可以阅读上述 Shaun 的 JWT 声明。如果您只是在寻找用户 ID(确保您已经使用 "Sub" 声明名称将其添加为声明的一部分),那么您可以根据您的用例使用以下两个示例来阅读:
读取用户 ID 声明:
public class AccountController : Controller
{
[Authorize]
[HttpGet]
public async Task<IActionResult> MethodName()
{
var userId = _userManager.GetUserId(HttpContext.User);
//...
return Ok();
}
}
阅读其他声明:
public class AccountController : Controller
{
[Authorize]
[HttpGet]
public async Task<IActionResult> MethodName()
{
var rolesClaim = HttpContext.User.Claims.Where( c => c.Type == ClaimsIdentity.DefaultRoleClaimType).FirstOrDefault();
//...
return Ok();
}
}
使用此解决方案,您可以在使用 JWT 令牌时访问 User.Identity
及其在控制器中的声明:
第 1 步:创建 JwtTokenMiddleware:
public static class JwtTokenMiddleware
{
public static IApplicationBuilder UseJwtTokenMiddleware(
this IApplicationBuilder app,
string schema = "Bearer")
{
return app.Use((async (ctx, next) =>
{
IIdentity identity = ctx.User.Identity;
if (identity != null && !identity.IsAuthenticated)
{
AuthenticateResult authenticateResult = await ctx.AuthenticateAsync(schema);
if (authenticateResult.Succeeded && authenticateResult.Principal != null)
ctx.User = authenticateResult.Principal;
}
await next();
}));
}
}
第 2 步:在 Startup.cs 中使用它:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAuthentication();
app.UseJwtTokenMiddleware();
}