【发布时间】:2017-03-31 12:31:41
【问题描述】:
我指的是Multitenant-saas-app 示例。我正在尝试获取访问令牌以访问 Graph API,然后静默获取访问令牌并再次访问图形 API。
使用多租户应用的 /common 端点获取授权码,
private string resourceID = "https://graph.windows.net";
string authorizationRequest = String.Format(
"https://login.microsoftonline.com/common/oauth2/authorize?response_type=code&client_id={0}&resource={1}&redirect_uri={2}&state={3}",
Uri.EscapeDataString(ConfigurationManager.AppSettings["ida:ClientID"]),
Uri.EscapeDataString("https://graph.windows.net"),
Uri.EscapeDataString(this.Request.Url.GetLeftPart(UriPartial.Authority).ToString() + "/Onboarding/ProcessCode"),
Uri.EscapeDataString(stateMarker)
);
return new RedirectResult(authorizationRequest);
使用授权码重定向,(/Onboarding/ProcessCode)
ClientCredential credential = new ClientCredential(ConfigurationManager.AppSettings["ida:ClientID"],
ConfigurationManager.AppSettings["ida:Password"]);
AuthenticationContext authContext = new AuthenticationContext("https://login.windows.net/common/");
//Get token to access grapgh API
AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(
code, new Uri(Request.Url.GetLeftPart(UriPartial.Path)), credential, resourceID);
AuthenticationHelper.token = result.AccessToken;
这很好,我获得了访问令牌,我可以在其中访问租户的 AzureAD 资源。
ActiveDirectoryClient client = AuthenticationHelper.GetActiveDirectoryClient();
IPagedCollection<IUser> pagedCollection = await client.Users.ExecuteAsync();
现在我尝试从令牌缓存中获取令牌以进行离线访问。这次我为租户创建 AuthenticationContext。 (我也试过 /common ) 这会默默地给我一个新的访问令牌。
string resourceID = "https://graph.windows.net";
//Test
ClientCredential credential = new ClientCredential(ConfigurationManager.AppSettings["ida:ClientID"],
ConfigurationManager.AppSettings["ida:Password"]);
AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/mytenant.net");
var auth = await authContext.AcquireTokenAsync(resourceID, credential);
var newToken = auth.AccessToken;
//Set the token for this session
AuthenticationHelper.token = auth.AccessToken;
然后我尝试像以前一样访问 API,
ActiveDirectoryClient client = AuthenticationHelper.GetActiveDirectoryClient();
IPagedCollection<IUser> pagedCollection = await client.Users.ExecuteAsync();
我得到以下异常,
Error = "Authorization_RequestDenied": "权限不足 完成操作。”
我在这里做错了吗?
这是我的应用权限,
【问题讨论】:
-
当您使用多租户应用程序时,租户管理员需要授予对应用程序的访问权限。也许这就是问题所在?是否有任何进一步的错误消息详细信息?
标签: c# asp.net-mvc adal azure-ad-graph-api