我已经使用JwtSecurityToken生成了令牌,但是,当我将其添加到postman的请求标头中以在api中调用操作时,我遇到了以下异常:
System.IO.IOException:IDX10804:无法从以下位置检索文档:'http://localhost:51156/.well-known/openid-configuration'。 ---> System.Net.Http.HttpRequestException:响应状态代码未指示成功:404(未找到)。 在System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() 在Microsoft.IdentityModel.Protocols.HttpDocumentRetriever。
d__8.MoveNext() ---内部异常堆栈跟踪的结尾--- 在Microsoft.IdentityModel.Protocols.HttpDocumentRetriever。 d__8.MoveNext() ---从之前引发异常的位置开始的堆栈结束跟踪--- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1.ConfiguredTaskAwaiter.GetResult()
 at Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfigurationRetriever.<GetAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable
1.ConfiguredTaskAwaiter.GetResult()处 在Microsoft.IdentityModel.Protocols.ConfigurationManager`1。d__24.MoveNext()
这是我生成令牌的方式:(为简单起见,我暂时对一些值进行了硬编码,而不是从appsettings.json中读取)
[AllowAnonymous]
[HttpPost]
[Route("")]
public IActionResult RequestToken([FromBody] TokenRequest request)
{
if (request.Username == "Jon" && request.Password == "123456")
{
var claims = new[]
{
new Claim(ClaimTypes.Name, request.Username)
};
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_securityConfig.Key));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer: "http://localhost:51156/",
audience: "http://localhost:51156/",
claims: claims,
expires: DateTime.Now.AddDays(3),
signingCredentials: creds);
return Ok(new
{
token = new JwtSecurityTokenHandler().WriteToken(token)
});
}
return BadRequest("Could not verify username and password");
}
这是我的ConfigureService方法的方式:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = "http://localhost:51156/";
options.Audience = "http://localhost:51156/";
options.RequireHttpsMetadata = false;
});
}
任何建议或指导将不胜感激。