我有一个 ASP.NET MVC Web 应用程序,需要检查用户是否是 Azure Active Directory 中特定组的成员。为了实现这一点,我将使用 Microsoft Graph API,因此我从 here 下载了他们的示例来尝试一下。并让它运行良好。
我的下一步是使用我自己的 AppId、AppSecret 和 RedirectUri 运行它,这就是我遇到麻烦的地方。在 Azure 中,我转到 AAD 的“应用程序注册”,并确保应用程序已添加。我打开应用程序并复制 AppId 的“应用程序 ID”,创建一个 key 并将其用作 AppSecret。我检查了所有权限并按“授予权限”并将我的 URL 添加到回复 URL。我还将权限属性从 https://login.microsoftonline.com/common/v2.0 更改为至 https://login.windows.net/xxxxxx.onmicrosoft.com .
当我运行应用程序并按“登录”时,我将正确进入登录屏幕,但当我尝试登录时它会崩溃。在以下代码中运行 AcquireTokenByAuthorizationCodeAsync 时会发生崩溃:
AuthorizationCodeReceived = async (context) =>
{
var code = context.Code;
string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
ConfidentialClientApplication cca = new ConfidentialClientApplication(
appId,
redirectUri,
new ClientCredential(appSecret),
new SessionTokenCache(signedInUserID, context.OwinContext.Environment["System.Web.HttpContextBase"] as HttpContextBase));
string[] scopes = graphScopes.Split(new char[] { ' ' });
AuthenticationResult result = await cca.AcquireTokenByAuthorizationCodeAsync(scopes, code);
},
AuthenticationFailed = (context) =>
{
context.HandleResponse();
context.Response.Redirect("/Error?message=" + context.Exception.Message);
return Task.FromResult(0);
}
我在 AuthenticationFailed 中收到的错误消息如下所示:
AADSTS70001:此 API 版本不支持应用程序“xxxxxxxx-4f81-4508-8dcb-df5b94f2290f”。跟踪 ID:xxxxxxxx-d04c-4793-ad14-868810f00c00 相关 ID:xxxxxxxx-ab83-4805-baea-8f590991ec0c 时间戳:2017-06-13 10:43:08Z
有人可以向我解释一下错误消息的含义吗?我应该尝试不同的版本吗?我尝试过 Microsoft.Graph v1.3.0/Microsoft.Graph.Core v1.4.0 和 Microsoft.Graph v1.4.0/Microsoft.Graph.Core v1.5.0。
最佳答案
您正在尝试将 MSAL 与 v1 终结点结合使用。使用 ConfidentialClientApplication
可以明显看出这一点。您需要改用 ADAL(Azure AD 身份验证库)。 或者在 https://apps.dev.microsoft.com 注册 v2 应用程序.
您可以从 NuGet 获取 ADAL:https://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory/
您可以在此处找到使用 ADAL 的示例应用程序:https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-openidconnect
您的 OnAuthorizationCodeReceived 需要如下所示:
private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification context)
{
var code = context.Code;
ClientCredential credential = new ClientCredential(clientId, appKey);
string userObjectID = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
AuthenticationContext authContext = new AuthenticationContext(Authority, new NaiveSessionCache(userObjectID));
// If you create the redirectUri this way, it will contain a trailing slash.
// Make sure you've registered the same exact Uri in the Azure Portal (including the slash).
Uri uri = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path));
AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(code, uri, credential, graphResourceId);
}
关于azure - AADSTS70001 : Application is not supported for this API version,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44517883/