我正在尝试通过遵循此处的教程,在使用客户端凭据
的情况下实现委托授权
。由于我有一个API1资源调用另一个API2资源,因此该教程只有一页。
我已经实现了IExtensionGrantValidator
并复制了来自文档的代码,使用它们提供的类名,并添加了授予类型为委派
的客户端
。 但是,我不确定在哪里以及如何调用下面的方法。起初,我从客户端
中调用它,并尝试将最初获得的调用API1的JWT传递给DelegateAsync
方法,但是一直收到错误请求
。
在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
方法,而它不清楚应该放在哪里。
Authorization
标头中的第二个单词(第一个单词始终为bearer
)。 - d_fHttpContext
获取了原始访问令牌用于调用我们已经移动到聊天中的第二个 API。 - user1574598