【问题标题】:Azure AD graph api working on local but fails when deployedAzure AD 图形 api 在本地工作,但在部署时失败
【发布时间】:2017-03-07 06:21:47
【问题描述】:

我尝试了https://github.com/Azure-Samples/active-directory-dotnet-graphapi-console/tree/master/GraphConsoleAppV3 的graphapi 代码。它适用于我的本地系统。在本地机器上,它会弹出一个窗口并要求登录。但是当我将应用程序部署到 azure web 门户时,它在获取令牌时失败了。

“调用 COM 组件返回错误 HRESULT E_FAIL” [COMException (0x80004005): 调用 COM 组件返回了错误 HRESULT E_FAIL。]

我认为这是从本地系统搜索令牌。我的令牌检索选项是否与 Windows 或 Web 相关?有关代码更改的任何建议。

如何在部署时替换此应用程序以使其正常工作。我想如果我们可以改变 ITenantDetailtenantDetail = 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;
        }

【问题讨论】:

  • “部署到 Azure 门户网站”是什么意思。示例是控制台应用程序?
  • 将控制台应用更新为 Web 应用并发布。
  • 在这种情况下,我认为 COM 异常源于代码试图启动 IE 实例以显示登录对话框。您可能应该使用其中一个 Web 应用示例作为您的起点(例如在 Fei Xue's answer 中)。
  • 如果您正在开发 Web 应用程序,由于代码在服务器端运行,因此不会向最终用户提示对话框。在这种情况下,请参考我帖子中的代码示例。如果您有任何问题,请随时告诉我。

标签: c# asp.net-mvc azure-web-app-service azure-active-directory azure-ad-graph-api


【解决方案1】:

代码示例中使用的Active Directory Authentication Library 是 帮助开发人员在包括 Windows 桌面、Windows 应用商店、Xamarin iOS 和 Xamarin Android 在内的各种平台上为您的 .NET 客户端使用身份验证功能。

如果您正在开发网络应用程序,请参考代码示例active-directory-dotnet-webapp-openidconnect。如果您还想在 Web 应用中使用 Azure AD 图形 API,可以参考代码示例active-directory-dotnet-graphapi-web。

Microsoft 还提供了许多使用 Azure 进行开发的示例,您可以从以下链接中找到它们:

Azure Samples

【讨论】:

    【解决方案2】:

    您的意思是用于登录的弹出窗口在 localhost 上工作正常,但在部署时不弹出?解决方法请参考此链接azure login popup not working

    您必须使用 powershell 登录。如果我误解了您的问题,请纠正我。

    【讨论】:

      猜你喜欢
      • 2016-06-20
      • 1970-01-01
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 2012-09-17
      • 2013-01-05
      相关资源
      最近更新 更多