riganti / dotvvm

Open source MVVM framework for Web Apps
https://www.dotvvm.com
Apache License 2.0
742 stars 97 forks source link

Azure authentication doesn't work in .net core 3.1 #794

Open PavelBansky opened 4 years ago

PavelBansky commented 4 years ago

When creating app from DotVVM wizard in VS2019 that is using Azure AD authentication. The app won't show any content when started. I think it's because .NET Core 3.1 changed the way the authentication is handled.

This is how ConfigureServices method looks in .net core 3.1 ASP MVC project

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

            services.AddControllersWithViews(options =>
            {
                var policy = new AuthorizationPolicyBuilder()
                    .RequireAuthenticatedUser()
                    .Build();
                options.Filters.Add(new AuthorizeFilter(policy));
            });

DotVVM: 2.2.155.0 VS 2019 .NET Core: 3.1

tomasherceg commented 4 years ago

Thank you for reporting the issue, we'll look at what has changed.

PavelBansky commented 4 years ago

I think I resolved it.

First issue: The template for project has incorrect version of Microsoft.AspNetCore.Authentication.OpenIdConnect. It needs to be version 3.?.? not 2.2

Second issue: After the version issue being resolved. Application ends with exception at following line DotvvmAuthenticationHelper.ApplyRedirectResponse(context.HttpContext, context.ProtocolMessage.BuildRedirectUrl()); in Startup.cs

Solution: I have added reference to Microsoft.AspNetCore.Authentication.AzureAD.UI version 3.1.2

And replaced the services.AddAuthnetication(sharedOptions => ... with following code

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

Also appsettings.json needs to be in the following format

  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "<domain>.onmicrosoft.com",
    "TenantId": "....011db47...",
    "ClientId": "....718bbc0a...",
    "CallbackPath": "/signin-oidc"
  }

Everything seems to be working fine except for this: ClaimsPrincipal.Current is null in ViewModels, but Context.HttpContext.User contains correct identity.

The question is if ClaimsPrincipal.Current not being populated represent issue for Dotvvm functionality.

andreyhlb commented 4 years ago

Hi, Can you share your url reply in Azure AD, please? Thanks.

Andrey

PavelBansky commented 4 years ago

How do I captured that reply? The auth is working now, as I posted.