【发布时间】:2020-03-29 19:14:45
【问题描述】:
我正在尝试通过遵循HERE 中的教程来弄清楚如何实现delegation 授权类型和client credentials,这实际上是一页,因为我有API1 资源调用另一个API2 资源.
我已经实现了IExtensionGrantValidator,并使用他们提供的类名从文档中复制了代码,并添加了client,授权类型为delegation。但是,我不确定在哪里以及如何在下面调用此方法,起初我是从 client 调用它并尝试将我最初调用 API1 的 JWT 传递给 DelegateAsync 方法,但我一直得到一个 @987654329 @
在 API 1 中,您现在可以自己构建 HTTP 负载,或使用 IdentityModel 帮助程序库:
public async Task<TokenResponse> DelegateAsync(string userToken)
{
var payload = new
{
token = userToken
};
// create token client
var client = new TokenClient(disco.TokenEndpoint, "api1.client", "secret");
// send custom grant to token endpoint, return response
return await client.RequestCustomGrantAsync("delegation", "api2", payload);
}
所以,我尝试从 API1 请求一个名为 GetAPI2Response 的方法中的令牌,该方法尝试调用 API2 中的方法:
[HttpGet]
[Route("getapi2response")]
public async Task<string> GetApi2Response()
{
var client = new HttpClient();
var tokenResponse = await client.RequestTokenAsync(new TokenRequest
{
Address = "http://localhost:5005/connect/token",
GrantType = "delegation",
ClientId = "api1_client",
ClientSecret = "74c4148e-70f4-4fd9-b444-03002b177937",
Parameters = { { "scope", "stateapi" } }
});
var apiClient = new HttpClient();
apiClient.SetBearerToken(tokenResponse.AccessToken);
var response = await apiClient.GetAsync("http://localhost:6050/api/values");
if (!response.IsSuccessStatusCode)
{
Debug.WriteLine(response.StatusCode);
}
else
{
var content = await response.Content.ReadAsStringAsync();
return content;
}
return "failed";
}
但是,在调试 invalid grant type 时会返回。奇怪的是,我注意到在运行 IDSRV 时,IExtensionGrantValidator 方法中的代码不会被命中,直到您单击 discovery docs 的链接,它才会显示为 grant type
我显然做错了,因为我没有包括前面提到的文档中的 DelegateAsync 方法,因为我不清楚它的去向。
【问题讨论】:
标签: asp.net-core identityserver4