使用 mvc dotnet core 访问 JWT 的有效负载
我在访问 dotnet 核心控制器中的 JWT(有效负载)时遇到问题。我不知道我错在哪里。我想我在下面的描述中涵盖了所有要点。如果我错过了一些可能有帮助的内容,请告诉我。 感谢您抽出时间。
将身份验证服务添加到 servicecollection
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = false,
ValidateIssuerSigningKey = false,
ValidIssuer = null,
ValidAudience = null,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("testify"))
};
});
我用于请求的令牌:
控制器动作的代码:
[HttpPost]
[ProducesResponseType(typeof(void), 201)]
[ProducesResponseType(typeof(void), 400)]
[ProducesResponseType(typeof(void), 401)]
[ProducesResponseType(typeof(void), 403)]
[ApiExplorerSettings(GroupName = "AuditLog")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public IActionResult Insert([Required] Dto.Log auditLog) => RunSafely(() =>
{
var log = _mapper.Map<Dto.Log, Log>(auditLog);
log.CorrelationId = _headerReader.GetCorrelationId(Request?.Headers);
_logRepository.AddLog(log);
return this.StatusCode((int)HttpStatusCode.Created);
});
I have a problem with accessing the (payload of) JWT in a dotnet core controller. I don't know where I am wrong. I think I covered all the important points in the following description. If I have missed something, that could help, please let me know.
Thank you for your time.
Adding authentication service to the servicecollection
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = false,
ValidateIssuerSigningKey = false,
ValidIssuer = null,
ValidAudience = null,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("testify"))
};
});
The token I used for the request:
The code of the controller action:
[HttpPost]
[ProducesResponseType(typeof(void), 201)]
[ProducesResponseType(typeof(void), 400)]
[ProducesResponseType(typeof(void), 401)]
[ProducesResponseType(typeof(void), 403)]
[ApiExplorerSettings(GroupName = "AuditLog")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public IActionResult Insert([Required] Dto.Log auditLog) => RunSafely(() =>
{
var log = _mapper.Map<Dto.Log, Log>(auditLog);
log.CorrelationId = _headerReader.GetCorrelationId(Request?.Headers);
_logRepository.AddLog(log);
return this.StatusCode((int)HttpStatusCode.Created);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过使用
System.Security.Claims.ClaimsPrincipal
命名空间内的User
对象来访问 jwt 的有效负载。例如:claims对象包含jwt的有效负载。此外,您还可以访问其他信息,例如:
You can access payload of jwt by using
User
object inside ofSystem.Security.Claims.ClaimsPrincipal
namespace. For example:The claims object contains payload of jwt. Also, you can access other information like:
你的问题对我来说也有点不清楚,我不确定你的代币的创建过程。然而,我的一个项目中有确切的场景,效果很好。以下是不同谜题的代码:
创建令牌:
对于 IServiceCollection :
和 config.json 文件:(请注意,由于安全原因,我更改了实际信息)
这些设置与我在一个小项目中使用的设置完全相同,并且工作正常。检查您的项目中是否缺少某些内容。
Your question is a little unclear to me, too, and I am not sure about the creation process of your token. However, I have the exact scenario in one of my projects, which works fine. Here is the code for different puzzles:
Creating the Token:
For IServiceCollection :
And config.json file: (Note that I changed real-world info due to security reasons)
These settings are exact that I'm using in one of my small projects, and It's working fine. Check if something is missing in your project or not.
在控制器中,您可以依赖
ihttpContextAccessor
,然后在httpcontext
上,调用扩展方法gettokenAsynaC(getTokenAsync(string)
,它将将返回编码的字符串。jwtsecuritytoken
在system.indesitymodel.tokens.jwt
namespace和getTokenAsync
中httpcontext
httpcontextMicrosoft.aspnetcore.authentication
名称空间。Within a controller, you can take a dependency on
IHttpContextAccessor
and then, on theHttpContext
, call the extension methodGetTokenAsync(string)
, which will return the encoded string.JwtSecurityToken
is in theSystem.IdentityModel.Tokens.Jwt
namespace, andGetTokenAsync
extension method ofHttpContext
is in theMicrosoft.AspNetCore.Authentication
namespace.原始有效载荷您可以访问RAW
访问令牌以使用.NET API控制器中的以下代码获取其
JWTPAYLOAD
JWT 提供更高级别的编程模型,默认情况下您永远无法访问JWT详细信息。这是一个值得关注的好模型 - 业务逻辑只能使用
索赔
:自定义JWT处理
如果要实现与JWT相关的自定义行为,则是标准使用框架的可扩展性。然后,管道进入
中间件
类,因此控制器类保持着专注于业务。我的几个示例类别显示了如何扩展.NET框架:有关更多信息,请参阅我的这些资源,这些资源专注于促进对API中重要标准的OAuth概念的理解:
RAW PAYLOAD
You can access the raw JWT access token to get its
JWTPayload
using the following type of code in a .NET API controller:CLAIMS
Microsoft provide a higher level programming model, where by default you never get access to the JWT details. This is a good model to follow - business logic should only ever use a
ClaimsPrincipal
:CUSTOM JWT PROCESSING
If you want to implement custom behaviour related to JWTs then it is standard to use the extensibility features of the framework. The plumbing then goes into a
middleware
class, so that controller classes stay business focused. A couple of example classes of mine show how to extend the .NET framework:For more info, see these resources of mine, which are focused on promoting understanding of the important standards based OAuth concepts in APIs:
访问有效负载的最简单方法是使用注入到类中的 IHttpContextAccessor ,如下所示。
您可以从标头中过滤身份验证令牌。
Easiest way to access payload is by using IHttpContextAccessor injected into your class like below.
From the headers you can filter for Auth token.