具有两个选项的Blazor服务器端应用程序身份验证

时间:2020-02-27 11:40:08

标签: authentication blazor-server-side

我对Blazor还是很陌生,并且正在努力实现一个提供两个登录选项的登录功能。该应用程序应接受通过AzureAD进行身份验证的我公司的用户,以及通过ASP Core Identity进行身份验证的外部用户。 通过身份登录已经设置。该应用程序为此使用默认逻辑。 我还可以切换到AzureAD身份验证,当我将其设置为唯一的提供程序时可以正常工作。

基本上我想关闭整个应用程序,并且必须对所有用户进行身份验证才能使用该应用程序。 他们应该被重定向到他们选择如何登录的登录页面。

Startup.cs:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<AppUser>(options =>
            {
                options.SignIn.RequireConfirmedAccount = true;
                options.User.RequireUniqueEmail = true;
            })
            .AddRoles<IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.AddRazorPages().AddRazorPagesOptions(options =>
        {
           options.Conventions.AuthorizeFolder("/");
        }).AddMvcOptions(options =>
        {
            var policy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .Build();
            options.Filters.Add(new AuthorizeFilter(policy));
        }); ;
        services.AddServerSideBlazor();
        services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<AppUser>>();

        services.AddScoped<ProjectService>();             
        services.AddScoped<AppUserService>();      

        services.AddTelerikBlazor();
        services.AddFileReaderService();
        services.AddTransient<IEmailSender, EmailSender>();
        services.AddScoped<UrlHelper>();

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

使用上述版本的Startup.cs,可以将用户正确重定向到/ Identity / Account / Login。可以通过身份登录。 当我尝试

return Challenge(new AuthenticationProperties{RedirectUri = returnUrl},
                    AzureADDefaults.AuthenticationScheme);

定向到AzureAD的用户可以在此处登录...,然后返回到该用户仍未被授权的登录页面。

当我从以下位置更改Startup.cs

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

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

用户直接转到AzureAD并在应用程序内通过身份验证。将没有进一步的重定向到登录页面。

有人知道我如何在登录页面上提供两种身份验证选项并强制用户重定向到该页面吗?

谢谢

0 个答案:

没有答案