I tried graphapi code from https://github.com/Azure-Samples/active-directory-dotnet-graphapi-console/tree/master/GraphConsoleAppV3. It worked on my local system. On local machine it pops up a window and ask for login. But When I deployed the application to azure web portal, it failed at the point where it gets the token sing Itenent.
"Error HRESULT E_FAIL has been returned from a call to a COM component" [COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.]
I think this is searching token from local system. Is my token retrieving option related to windows or web? Any suggestion on code changes.
How can I replace this application to work when deployed. I think if we can change the ITenantDetail tenantDetail = GetTenantDetailsSync(client, UserModeConstants.TenantId); code to one which gets info from user, this should work on web also.
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;
}