c# - Azure AD graph api 在本地工作但在部署时失败

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

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

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

我认为这是从本地系统搜索 token 。我的 token 检索选项与 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 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

关于c# - Azure AD graph api 在本地工作但在部署时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42641539/

相关文章:

asp.net - 在 F# 中开发 ASP.NET 和 ASP.NET MVC 应用程序的缺点?

javascript - 如何在 Highcharts C# 代码中编辑工具提示

Azure 应用程序配置、 key 保管库和托管服务身份 (.NET Core 3.1)

Azure 虚拟目录中的多个站点

azure - 如何从 Visual Studio 禁用应用程序服务的手动部署

c# - 有什么可以用 C# 事件做而不能用 VB 事件做的事情吗?

c# - Visual Studio 中的代码指标计算

asp.net-mvc - 如何使用 Moq 模拟存储库 Single(Expression<Func<TEntity, bool>> predicate) 方法

c# - ObjectSet<T>.AddObject() 与 EntityCollection<T>.Add()

c# - 单击 asp 按钮获取下拉列表的值