Azure AD 图 api 在本地工作但在部署时失败
Azure AD graph api working on local but fails when deployed
我尝试了 https://github.com/Azure-Samples/active-directory-dotnet-graphapi-console/tree/master/GraphConsoleAppV3 中的 graphapi 代码。它适用于我的本地系统。在本地机器上,它会弹出一个 window 并要求登录。但是当我将应用程序部署到 azure 门户网站时,它在获取令牌时失败了。
"Error HRESULT E_FAIL has been returned from a call to a COM component"
[COMException (0x80004005): 已从对 COM 组件的调用返回错误 HRESULT E_FAIL。]
我认为这是从本地系统搜索令牌。我的令牌检索选项与 windows 或网络相关吗?关于代码更改的任何建议。
部署后如何替换此应用程序以使其正常工作。我想如果我们可以更改 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 是
帮助开发人员在各种平台上为您的 .NET 客户端使用身份验证功能,包括 Windows 桌面、Windows 商店、Xamarin iOS 和 Xamarin Android.
如果您正在开发网络应用程序,请参考代码示例 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。
Microsoft 还提供了许多使用 Azure 进行开发的示例,您可以从下面找到它们 link:
你的意思是登录弹出窗口在本地主机上工作正常但在部署时不弹出?请参考此 link 以获取解决方案
如果我误解了你的问题,你必须为 login.correct 我使用 powershell。
我尝试了 https://github.com/Azure-Samples/active-directory-dotnet-graphapi-console/tree/master/GraphConsoleAppV3 中的 graphapi 代码。它适用于我的本地系统。在本地机器上,它会弹出一个 window 并要求登录。但是当我将应用程序部署到 azure 门户网站时,它在获取令牌时失败了。
"Error HRESULT E_FAIL has been returned from a call to a COM component" [COMException (0x80004005): 已从对 COM 组件的调用返回错误 HRESULT E_FAIL。]
我认为这是从本地系统搜索令牌。我的令牌检索选项与 windows 或网络相关吗?关于代码更改的任何建议。
部署后如何替换此应用程序以使其正常工作。我想如果我们可以更改 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 是 帮助开发人员在各种平台上为您的 .NET 客户端使用身份验证功能,包括 Windows 桌面、Windows 商店、Xamarin iOS 和 Xamarin Android.
如果您正在开发网络应用程序,请参考代码示例 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。
Microsoft 还提供了许多使用 Azure 进行开发的示例,您可以从下面找到它们 link:
你的意思是登录弹出窗口在本地主机上工作正常但在部署时不弹出?请参考此 link 以获取解决方案
如果我误解了你的问题,你必须为 login.correct 我使用 powershell。