简体   繁体   English

在 Blazor 中一起使用 ASP.Net Core Identity 和 Azure 身份验证

[英]Using ASP.Net Core Identity and Azure authentication together in Blazor

I have a server-side Blazor app which is successfully integrated with Azure for authentication.我有一个服务器端 Blazor 应用程序,它成功地与 Azure 集成以进行身份验证。 The standard approach worked here, I modified Startup.cs as follows:标准方法在这里工作,我修改 Startup.cs 如下:

services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
                    .AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"));
            services.AddControllersWithViews()
                    .AddMicrosoftIdentityUI();
            services.AddAuthorization(options => {
                // By default, all incoming requests will be authorized 
                // according to the default policy
                options.FallbackPolicy = options.DefaultPolicy;
            });

And then I check for authentication in MainLayout.razor as follows:然后我在 MainLayout.razor 中检查身份验证,如下所示:

    protected override async Task OnInitializedAsync()
    {
        AuthenticationState authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        System.Security.Claims.ClaimsPrincipal user = authState.User;
        System.Security.Principal.IIdentity userIdent = user.Identity;
        if (userIdent.IsAuthenticated)
        {
            doMoreStuff(user);
        }
        ...
    }

I would like to ALSO enable authorization via Identity with custom UserStore and RoleStore implementations (NOT using Entity framework core).我还想通过 Identity 使用自定义 UserStore 和 RoleStore 实现启用授权(不使用实体框架核心)。 I started to implement user store and role store, and added them to Startup.cs:我开始实现用户存储和角色存储,并将它们添加到 Startup.cs:

IdentityBuilder builder = services.AddIdentity<MyUser, MyRole>()
                .AddDefaultTokenProviders();            
builder.Services.AddScoped(typeof(IUserStore<>).MakeGenericType(builder.UserType), typeof(MyUserManager));
builder.Services.AddScoped(typeof(IRoleStore<>).MakeGenericType(builder.RoleType), typeof(MyRoleManager));

But as soon as I add these custom Identity additions, I immediately see the code in MainLayout calling AuthorizationStateProvider.GetAuthenticationStateAsync() now returns null.但是一旦我添加了这些自定义身份添加,我立即看到 MainLayout 中调用 AuthorizationStateProvider.GetAuthenticationStateAsync() 的代码现在返回 null。

Any suggestions on how to implement BOTH custom Identity AND the Azure authentication?关于如何实现自定义身份和 Azure 身份验证的任何建议? They don't want to play well together for me.他们不想为我打得很好。

Answered my own question, actually I found some good advice here .回答了我自己的问题,实际上我在这里找到了一些很好的建议。

The approach seems to be to just take the ClaimsPrincipal returned by Azure, and then add your own additional Claims from within the app.该方法似乎只是采用 Azure 返回的 ClaimsPrincipal,然后从应用程序中添加您自己的附加声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC身份-一起使用内部用户和Azure AD身份验证 - ASP.NET MVC Identity - Using internal User and Azure AD Authentication together Azure AD 身份验证对现有 ASP.NET 核心身份应用程序 - Azure AD authentication to existing ASP.NET Core Identity application 使用Firebase和Identity的ASP.NET Core API身份验证 - ASP.NET Core API authentication using Firebase and Identity 将 ASP.NET Core 标识与 IdentityServer4 结合使用 - 身份验证 - Using ASP.NET Core Identity with IdentityServer4 - Authentication Asp.net Identity使用密码和Azure Active Directory身份验证 - Asp.net Identity using password and Azure Active Directory authentication 如何将 Blazor Web 程序集与 ASP.NET 核心托管和身份验证应用程序发布到 ZCF04A02E37B774FC3917A 服务? - How to publish Blazor Web assembly with ASP.NET core hosting and identity auth app to azure App Service? Asp.Net Core 2.2身份验证 - Asp.Net Core 2.2 Identity Authentication ASP.NET 核心 WebApi 身份验证 - ASP.NET Core WebApi Authentication with Identity 使用 Windows 身份验证的 ASP.NET Core 标识 - ASP.NET Core Identity with Windows Authentication Blazor 服务器和 CleanCode - ASP.NET 核心标识 - Blazor Server and CleanCode - ASP.NET Core Identity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM