How to integrate Easy Auth with .NET Core 3.1 MVC App?

874 views Asked by At

I am developing a .NET Core 3.1 MVC App and then publishing it to Azure Web App. In the Azure portal, for this hosted app -> I enabled App Service Authentication with AzureAD Login. But then the authentication doesn't work as "User.Identity.IsAuthenticated" is always coming as false in the Controller and I can't fetch other user details I want to, like email etc. Upon searching I found there is a workaround using a Nuget Package for >Net Core 2.2 (https://github.com/MaximRouiller/MaximeRouiller.Azure.AppService.EasyAuth), but I don't see any solution for 3.1.

However, when I setup custom auth by disabling the App Service Authentication in Azure, and set the auth in Startup.cs like this:

services.AddAuthentication(AzureADDefaults.AuthenticationScheme).AddAzureAD(options => Configuration.Bind("AzureAd", options));

and this:

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => ...

with config in appsettings.json, the auth works fine and I can fetch user details as well.

But our preferred solution is not to have any auth settings/custom auth in code and rather handle it fully on the portal using Azure AD Easy Auth with .NET Core 3.1 MVC app. Would really appreciate any help.

1

There are 1 answers

0
Ryan Hill On

This is a known limitation for EasyAuth and .NET Core as documented. The User Principal is implemented differently in .NET Core and EasyAuth can't grab those details automagically like it can in .NETFX. That is why you need to use Maxime's workaround.