简体   繁体   中英

Deploying Blazor app to Azure using AAD Authentication

I've created a default Blazor App (Server-side) in Visual Studio 2019 with Azure AD authentication. Running the app locally it authenticates against AAD as expected. However, when deployed to an App Service in Azure the app won't run. The deployment process works fine, Deployment Mode is Self-Contained and App Service is Windows (Linux works the same way). All files and settings are made appropriately, but the below error is displayed when navigating to the site.

This page isn't working blazorappxxxxxxx.azurewebsites.net is currently unable to handle this request.

HTTP ERROR 500

Disable the authentication code in startup.cs and redeploy, now the app works.

        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));
        });

The App is registered in AAD and Redirect URIs are set, implicit grant is configured for Access Token and ID Token.

Try to use the code as below:

public void ConfigureServices(IServiceCollection services)  
{  
    services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
         .AddAzureAD(options => Configuration.Bind("AzureAd", options)); 
    services.AddControllers();  
}  

Behind the scenes, Visual Studio automatically registers our ASP.NET Core app in Azure AD App Registration. For more details, you could refer to this tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM