簡體   English   中英

Azure AD圖形api在本地工作但在部署時失敗

[英]Azure AD graph api working on local but fails when deployed

我嘗試了https://github.com/Azure-Samples/active-directory-dotnet-graphapi-console/tree/master/GraphConsoleAppV3中的 graphapi代碼。 它適用於我的本地系統。 在本地計算機上,它彈出一個窗口並要求登錄。 但是當我將應用程序部署到azure Web門戶時,它在獲取令牌的時候失敗了Itenent。

“錯誤HRESULT E_FAIL已從調用COM組件返回”[COMException(0x80004005):錯誤HRESULT E_FAIL已從調用COM組件返回。

我認為這是從本地系統搜索令牌。 我的令牌檢索選項是否與Windows或Web相關? 有關代碼更改的任何建議。

如何在部署時替換此應用程序以使其工作。 我想如果我們可以改變ITenantDetail tenantDetail = GetTenantDetailsS​​ync(client,UserModeConstants.TenantId); 代碼從一個從用戶獲取信息,這也應該在網絡上工作。

private static ActiveDirectoryClient client;
client = AuthenticationHelper.GetActiveDirectoryClientAsUser();
ITenantDetail tenantDetail = GetTenantDetailsSync(client, UserModeConstants.TenantId);



 public static ITenantDetail GetTenantDetailsSync(IActiveDirectoryClient client, string tenantId)
    {
        ITenantDetail tenant = null;
        try
        {
            IPagedCollection<ITenantDetail> tenantsCollection = client.TenantDetails
                .Where(tenantDetail => tenantDetail.ObjectId.Equals(tenantId)).ExecuteAsync().Result;

            List<ITenantDetail> tenantsList = tenantsCollection.CurrentPage.ToList();

            if (tenantsList.Count > 0)
            {
                tenant = tenantsList.First();
            }
        }
        catch (Exception ex)
        {
        }

        if (tenant == null)
        {
            return null;
        }
        else
        {
            TenantDetail tenantDetail = (TenantDetail)tenant;
            return tenantDetail;
        }
    }



public static ActiveDirectoryClient GetActiveDirectoryClientAsUser()
        {
            Uri servicePointUri = new Uri(GlobalConstants.ResourceUrl);
            Uri serviceRoot = new Uri(servicePointUri, UserModeConstants.TenantId);
            ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot,
                async () => await AcquireTokenAsyncForUser());
            return activeDirectoryClient;
        }

public static async Task<string> AcquireTokenAsyncForUser()
        {
            return await GetTokenForUser();
        }

public static async Task<string> GetTokenForUser()
        {
            if (TokenForUser == null)
            {
                var redirectUri = new Uri("https://localhost");
                AuthenticationContext authenticationContext = new AuthenticationContext(UserModeConstants.AuthString, false);
                AuthenticationResult userAuthnResult = await authenticationContext.AcquireTokenAsync(GlobalConstants.ResourceUrl,
                    UserModeConstants.ClientId, redirectUri, new PlatformParameters(PromptBehavior.RefreshSession));
                TokenForUser = userAuthnResult.AccessToken;
            }
            return TokenForUser;
        }

代碼示例中使用的Active Directory身份驗證庫可幫助開發人員在各種平台上使用.NET客戶端的身份驗證功能, 包括Windows桌面,Windows應用商店,Xamarin iOS和Xamarin Android

如果您正在開發Web應用程序,請參閱代碼示例active-directory-dotnet-webapp-openidconnect 如果您還想在Web應用程序中使用Azure AD圖形API,則可以參考代碼示例active-directory-dotnet-graphapi-web

Microsoft還提供了大量用Azure開發的示例,您可以從以下鏈接中找到它們:

Azure示例

你的意思是用於登錄的彈出窗口在localhost上正常工作但在部署時不會彈出 請參閱此鏈接,解決方案azure登錄彈出窗口無法正常工作

如果我誤解了你的問題,你必須使用powershell for login.correct me。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM