I registered my app on Azure AD where I got ClientId, TenantId, etc. Since my app is a web api I test to get access token as described here https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-implicit-grant-flow and successfully obtain the access token, I test the token using Postman. Unfortunately, the app cannot validate the token that the User.Identity.IsAuthenticated always returns false. When I check the User property it is empty and contains no claims such as email etc even if I set it in Azure.
var builder = WebApplication.CreateBuilder(args);
var configuration = builder.Configuration;
var services = builder.Services;
services.AddMicrosoftIdentityWebApiAuthentication(configuration);
services.AddControllers();
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthentication();
app.Use(async (context, next) =>
{
bool isAuthenticated = context.User?.Identity?.IsAuthenticated == true; //always false even if the authorization bearer token exists
if (!isAuthenticated)
{
//more logics here
}
else
{
await next();
}
});
app.MapControllers();
app.Run();