rest - JWT 授权令牌在客户端生成,但在 API 中不起作用
问题描述
我已经尝试实现jwt 身份验证,因为令牌是在客户端正确生成的,但Web API没有对来自客户端的请求进行身份验证,我提供了我使用过的代码,我正在用asp 实现这个。净核心 1.1。 我已确保令牌正在使用JWT.io生成有效信息,并且一切正常。
这是 Web API 上的代码,它将验证来自另一台服务器的传入令牌
app.UseJwtBearerAuthentication(new JwtBearerOptions()
{
AutomaticAuthenticate=true,
IncludeErrorDetails=true,
TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = false,
ValidateAudience = false,
ValidAudience = "exampleissuer",
ValidIssuer = "exampleaudience",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("superkey_ValueforSigningTheJSONWebTokenJWT"))
}
});
这是应用程序中生成令牌并通过请求发送的代码。
var signinKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes("superkey_ValueforSigningTheJSONWebTokenJWT"));
var token = new JwtSecurityToken(
issuer: "exampleissuer",
audience: "exampleaudience",
signingCredentials: new SigningCredentials(signinKey, SecurityAlgorithms.HmacSha256)
);
var AuthenticationToken = new JwtSecurityTokenHandler().WriteToken(token);
//
_restClient.Authenticator = new JwtAuthenticator(AuthenticationToken);
req.AddHeader("WWW-Authenticate", string.Format("Bearer {0}", AuthenticationToken));
_restClient.Authenticator.Authenticate(_restClient, req);
//
有人可以帮我在 web api 中验证令牌。
解决方案
推荐阅读
- multithreading - 如何中断正在等待 C++ 中的 std::condition_variable_any 的线程?
- sql - 批量收集的订阅数不胜数
- matlab - 在 Simulink 仿真中访问和更改模型工作区中的变量
- python - 如何将函数调用到文件中
- swiftui - SwiftUI - 如何在 iOS 13 中制作文本导航链接的一部分?
- r - 如何摆脱错误“输入中没有可用的行”?
- sql - PostgreSQL:如何修改视图中的值
- python - Python CVXPY:属性错误 - 参数
- kotlin - 使用 Mockk 和扩展函数的类型安全方式
- python - 在这种情况下,.capitalize() 在 Python 中不起作用