[英]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.他们不想为我打得很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.