我尝试了 3 种方法都没有结果:
- 根据这篇文章https://msdn.microsoft.com/en-us/library/azure/ee460782.aspx我已经在 AAD 中注册了具有访问 Azure 服务管理 API 权限的新 Web 应用程序(步骤 1-9)并编写了推荐的两行代码来获取令牌:
var context = new AuthenticationContext($"https://login.windows.net/{tenantId}");
var result = context.AcquireToken("https://management.core.windows.net/", clientId, new Uri(redirectUri));
,但它失败了,但有以下例外:
Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException was unhandled
Message: An unhandled exception of type 'Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException' occurred in Microsoft.IdentityModel.Clients.ActiveDirectory.dll
Additional information: AADSTS90014: The request body must contain the following parameter: 'client_secret or client_assertion'.
Trace ID: aa2d6962-5aea-4f8e-bed4-9e83c7631887
Correlation ID: f7f1a61e-1720-4243-96fa-cff182150931
- 我也试过:
var context = new AuthenticationContext($"https://login.windows.net/{tenantId}");
var result = context.AcquireToken("https://management.core.windows.net/", new ClientCredential(clientId, clientSecret));
其中 clientSecret 是我的应用程序的秘密应用程序密钥。这个版本返回一个token,但是使用这个token的请求返回403 Forbidden:服务器验证请求失败。验证证书是否有效并且与此订阅相关联。
var context = new AuthenticationContext(string.Format("https://login.windows.net/{0}", tenantId));
// TODO: Replace with your Azure AD user credentials (i.e. admin@contoso.onmicrosoft.com)
string user = "{YOUR-USERID]";
string pwd = "{YOUR-USER-PASSWORD}";
var userCred = new UserCredential(user, pwd);
AuthenticationResult result =
await context.AcquireTokenAsync("https://management.core.windows.net/", clientId, userCred);
但它也失败了,与第一种情况相同......
你能帮帮我吗?