IdentityServer4 客户端模式
成功拿到 token 了
请求接口 却返回 404 (头部有带 token 参数 )
AuthenticationScheme: Bearer was challenged.
var client = new HttpClient();
var response = await client.RequestTokenAsync(new TokenRequest
{
Address = "http://localhost:5000/connect/token",
GrantType = "client_credentials",
ClientId = "client",
ClientSecret = "secret",
Parameters =
{
{ "custom_parameter", "custom value"},
{ "scope", "api1" }
}
});
var r = response.AccessToken;
var client2 = new HttpClient();
client2.SetBearerToken(r);
var response2 = await client2.GetAsync("http://localhost:5001/WeatherForecast");
response2 返回 401
SetBearerToken实现是什么?
请求的 头部 token
@心雨纷扬:
id4 https://github.com/conanl5566/dotnet-core-Example/tree/master/WebApplication25
api https://github.com/conanl5566/dotnet-core-Example/tree/master/WebApplication26
测试客户端 https://github.com/conanl5566/dotnet-core-Example/tree/master/ConsoleApp7
@conan_lin: 服务器配置问题
服务器加入一下配置
services.AddIdentityServer()
.AddJwtBearerClientAuthentication()
public static class Test
{
/// <summary>
/// Adds support for client authentication using JWT bearer assertions.
/// </summary>
/// <param name="builder">The builder.</param>
/// <returns></returns>
public static IIdentityServerBuilder AddJwtBearerClientAuthentication(this IIdentityServerBuilder builder)
{
builder.AddSecretParser<JwtBearerClientAssertionSecretParser>();
builder.AddSecretValidator<PrivateKeyJwtSecretValidator>();
return builder;
}
}
东西有点多,我提交下吧
给你推了一个pullrequest,不过改动有点多,因为我删除了不相干的项目,你也可以看我的
http://localhost:5001/WeatherForecast
这个服务配置如何验证token了吗?不然肯定401啊
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:5000"; //配置Identityserver的授权地址
options.RequireHttpsMetadata = false; //不需要https
options.ApiName = "a"; //api的name,需要和config的名称相同
});
[Authorize]
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
@conan_lin: 那有具体点的错误提示吗,401看起来是token有误,或者是5001服务在验证token的时候出错了。 查下5001的在接受到api请求的详细log