I've an ASP.NET Core web application with multiple authentication schemes.
The default scheme is Cookie:
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(options =>
{
configuration.Bind("Authentication:AzureAd", options);
options.Events.OnAuthorizationCodeReceived = authorizationCodeReceivedContext =>
{
AuthHelper.IsAzureAdAuthenticated = true;
return Task.CompletedTask;
};
})
.EnableTokenAcquisitionToCallDownstreamApi()
.AddMicrosoftGraph(configuration.GetSection("Authentication:Graph"))
.AddSessionTokenCaches();
services.AddRazorPages().AddMicrosoftIdentityUI();
My "Test" page can be accessed by any users, with form or Azure AD account.
If the user has an Azure AD account I use GraphServiceClient to get the unread email count.
The "Test" page:
[Authorize]
public class GraphCMSModel : PageModel
{
private readonly GraphLogic _graphLogic;
public string AreaContent { get; set; } = string.Empty;
public GraphCMSModel(GraphLogic graphLogic)
{
_graphLogic = graphLogic;
}
public async Task OnGetAsync()
{
if (AuthHelper.IsAzureAdAuthenticated)
{
AreaContent = await _graphLogic.GetContentAsync();
}
else
{
AreaContent = "No Graph for you";
}
}
}
GraphLogic has GraphServiceClient:
public class GraphLogic
{
private readonly GraphServiceClient _graph;
public GraphLogic(GraphServiceClient graph)
{
_graph = graph;
}
public async Task<string> GetContentAsync()
{
var messages = await _graph.Me.MailFolders["Inbox"].Messages
.Request()
.Filter("isRead ne true")
.GetAsync();
return $"You have {messages.Count} unread messages";
}
}
I get this error:
InvalidOperationException: IDW10503: Cannot determine the cloud Instance. The provided authentication scheme was ''. Microsoft.Identity.Web inferred 'Cookies' as the authentication scheme. Available authentication schemes are 'Cookies,OpenIdConnect'.
I tried several things:
- with multiple Authorize attributes or multiple schemas in authorize attribute: same error
- if OpenConnectId is the default Schema Graph works, but I've problem with users logged-in with form: thet get redirected to Azure login page
- I don't know how to configure GraphServiceClient in other ways