Office 365 API Microsoft Graph身份验证失败

时间:2016-04-26 13:51:11

标签: c# .net active-directory office365 office365api

我从Microsoft graph sample app

注册了示例应用

标准登录正在运行,但当我尝试使用此代码使其更简单时:

        var authContext = new AuthenticationContext(Settings.AzureADAuthority);

        var token = authContext.AcquireToken(Settings.O365UnifiedAPIResource, new ClientCredential(Settings.ClientId, Settings.ClientSecret)).AccessToken;

我收到以下错误:Application with identifier '[ClientId here]' was not found in the directory microsoft.com

Setting.O365UnifiedAPIResource = @"https://graph.microsoft.com/";
Settings.AzureADAuthority = @"https://login.microsoftonline.com/common";

有谁知道会出现什么问题?

1 个答案:

答案 0 :(得分:2)

Settings.AzureADAuthority = @"https://login.microsoftonline.com/{tenant_id or tenant_name}";

使用客户端凭证(客户端ID +客户端密钥)获取令牌时。您应该明确指定租户。

例如:

https://login.microsoftonline.com/ {tenant_id}

https://login.microsoftonline.com/ {your_domain.onmicrosoft.com}

顺便说一句,由于此注册将针对示例应用程序,因此它将仅具有被委派权限的Mail.Send权限。要获取应用令牌,您还需要在Azure AD中授予应用级权限,因为您正在获取应用令牌而非用户令牌。

enter image description here