我尝试了3种方法,但没有结果:
- 根据这篇文章https://msdn.microsoft.com/en-us/library/azure/ee460782.aspx我已在 AAD 中注册了新的 Web 应用程序,并具有访问 Azure 服务管理 API 的权限(步骤 1-9),并编写了建议的两行代码来获取 token :
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 是我的应用程序的 secret 应用程序 key 。 此版本返回一个 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. <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3a2a7aeaaad83a0acadb7acb0acedacadaeaaa0b1acb0aca5b7eda0acae" rel="noreferrer noopener nofollow">[email protected]</a>)
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);
但它也会失败,并出现与第一种情况相同的异常......
你能帮我一下吗?
最佳答案
在 Azure 门户中创建应用程序时,应将“应用程序类型”更改为“ native 客户端应用程序”。
关于c# - 如何通过 AAD 对 Azure 服务管理请求进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33305581/