简体   繁体   English

Azure AD图形api在本地工作但在部署时失败

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

I tried graphapi code from https://github.com/Azure-Samples/active-directory-dotnet-graphapi-console/tree/master/GraphConsoleAppV3 . 我尝试了https://github.com/Azure-Samples/active-directory-dotnet-graphapi-console/tree/master/GraphConsoleAppV3中的 graphapi代码。 It worked on my local system. 它适用于我的本地系统。 On local machine it pops up a window and ask for login. 在本地计算机上,它弹出一个窗口并要求登录。 But When I deployed the application to azure web portal, it failed at the point where it gets the token sing Itenent. 但是当我将应用程序部署到azure Web门户时,它在获取令牌的时候失败了Itenent。

"Error HRESULT E_FAIL has been returned from a call to a COM component" [COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.] “错误HRESULT E_FAIL已从调用COM组件返回”[COMException(0x80004005):错误HRESULT E_FAIL已从调用COM组件返回。

I think this is searching token from local system. 我认为这是从本地系统搜索令牌。 Is my token retrieving option related to windows or web? 我的令牌检索选项是否与Windows或Web相关? Any suggestion on code changes. 有关代码更改的任何建议。

How can I replace this application to work when deployed. 如何在部署时替换此应用程序以使其工作。 I think if we can change the ITenantDetail tenantDetail = GetTenantDetailsSync(client, UserModeConstants.TenantId); 我想如果我们可以改变ITenantDetail tenantDetail = GetTenantDetailsS​​ync(client,UserModeConstants.TenantId); code to one which gets info from user, this should work on web also. 代码从一个从用户获取信息,这也应该在网络上工作。

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;
        }

The Active Directory Authentication Library using in the code sample is help developers to use authentication functionality for your .NET client on various platforms including Windows desktop, Windows Store, Xamarin iOS and Xamarin Android . 代码示例中使用的Active Directory身份验证库可帮助开发人员在各种平台上使用.NET客户端的身份验证功能, 包括Windows桌面,Windows应用商店,Xamarin iOS和Xamarin Android

If you were developing an web app, please refer the code sample active-directory-dotnet-webapp-openidconnect . 如果您正在开发Web应用程序,请参阅代码示例active-directory-dotnet-webapp-openidconnect And if you also want to use the Azure AD graph API in the web app, you can refer the code sample active-directory-dotnet-graphapi-web . 如果您还想在Web应用程序中使用Azure AD图形API,则可以参考代码示例active-directory-dotnet-graphapi-web

Microsoft also provide lots of samples to develop with Azure, you can find them from the below link: Microsoft还提供了大量用Azure开发的示例,您可以从以下链接中找到它们:

Azure Samples Azure示例

you mean popup for login works fine on localhost but not popping up when deployed? 你的意思是用于登录的弹出窗口在localhost上正常工作但在部署时不会弹出 please refer this link for the solution azure login popup not working 请参阅此链接,解决方案azure登录弹出窗口无法正常工作

you have to use powershell for login.correct me if i misunderstood your question. 如果我误解了你的问题,你必须使用powershell for login.correct me。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM