Kur*_*ula 4 c# asp.net-mvc azure-web-sites azure-active-directory azure-ad-graph-api
我尝试了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 = 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;
}
Run Code Online (Sandbox Code Playgroud)
代码示例中使用的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开发的示例,您可以从以下链接中找到它们: