在ASP.NET4.7 MVC应用程序中获取Identity Server4的access_token,可以按照以下步骤进行:
var client = new HttpClient();
var disco = await client.GetDiscoveryDocumentAsync("https://identityserver-url");
if (disco.IsError)
{
// 处理错误
}
var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = disco.TokenEndpoint,
ClientId = "client-id",
ClientSecret = "client-secret",
Scope = "scope"
});
if (tokenResponse.IsError)
{
// 处理错误
}
var access_token = tokenResponse.AccessToken;
在上述代码中,需要将以下信息替换为实际的配置值:
需要注意的是,上述代码仅适用于使用客户端凭据授权模式(Client Credentials Grant)获取access_token。如果需要其他授权模式(如授权码模式、密码模式等),则需要相应调整代码。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云