【发布时间】:2020-03-04 19:01:31
【问题描述】:
我正在尝试使用用户凭据检索访问令牌。
我正在使用 AcquireTokenAsync 方法来检索令牌,其中我使用带有资源、客户端 ID 和用户凭据作为参数的构造函数。
public async Task<IHttpActionResult> GetToken()
{
AuthenticationResult authenticationResult = null;
try
{
string authority = "https://login.microsoftonline.com/tenant";
string resource ="2424-234-234234-234-23-32423";
string username = "yxyzzz";
string password = "password";
string clientId="2424-234-234234-234-23-32423";
var useridpassword = new UserPasswordCredential(username, password);
AuthenticationContext context = new AuthenticationContext(authority);
context.TokenCache.Clear();
authenticationResult = await context.AcquireTokenAsync(resource, clientId, useridpassword);
return authenticationResult.AccessToken;
}
catch (Exception ex)
{
throw ex;
}
}
我希望返回访问令牌,但在获取令牌时出现异常。以下是我收到的错误消息。
AdalException: {"error":"invalid_client","error_description":"AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.\r\nTrace ID: 674f29fe-73c6-49a3-9c3f-24df4ea16000\r\nCorrelation ID: b14cb535-9df5-48fa-b911-7e8b927fceb7\r\nTimestamp: 2019-11-08 06:21:57Z","error_codes":[7000218],"timestamp":"2019-11-08 06:21:57Z","trace_id":"674f29fe-73c6-49a3-9c3f-24df4ea16000","correlation_id":"b14cb535-9df5-48fa-b911-7e8b927fceb7","error_uri":"https://login.microsoftonline.com/error?code=7000218"}: Unknown error
【问题讨论】:
-
这个问题有什么更新吗?
-
是的.. 可以通过调用 Oauth2/token 端点来获取令牌。
-
我已经分享了对我来说很好用的代码。
-
感谢您的分享,您可以将其作为答案。那么这个issue就可以关闭了。谢谢。
标签: asp.net azure access-token azure-authentication