我尝试了来自 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 = GetTenantDetailsSync(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 进行开发的示例,您可以从以下链接找到它们:
关于c# - Azure AD graph api 在本地工作但在部署时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42641539/