我指的是Multitenant-saas-app示例。我嘗試獲取訪問令牌以訪問Graph API,然後以靜默方式訪問令牌並再次訪問圖形API。AzureAD multiteenant app - 「Authorization_RequestDenied」:「沒有足夠的權限來完成操作
獲取授權碼與用於多租戶應用內/公共端點,
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);
與授權碼重定向,(/入職培訓/ 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。 (我也試過/共同點) 這讓我默默地接受了一個新的accesstoken。
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();
,我得到下面的異常,
錯誤= 「Authorization_RequestDenied」:「特權才能 不足完成操作「。
我在這裏做錯了什麼嗎?
這裏是我的應用程序的權限,
當您使用多租戶應用程序時,租戶管理員需要授予對應用程序的訪問權限。也許這就是問題所在?是否有任何進一步的錯誤消息細節? – RasmusW