【问题标题】:Custom User data in ASP.NET MVC Core 3.1 with Azure AD Authentication?使用 Azure AD 身份验证的 ASP.NET MVC Core 3.1 中的自定义用户数据?
【发布时间】:2021-10-02 16:34:34
【问题描述】:

我们正在迁移旧版网络应用,但希望保留存储在数据库中的特定于应用的用户信息。我们已经成功地针对我们的新 Azure AD 设置了身份验证——我们正在使用具有多个租户的 OIDC。用户登录后,我们将通过他们的电子邮件地址从数据库中查找相关的用户记录。问题在于将我们现有的用户数据映射到ClaimPrincipal。文档声称我们可以扩展 IdentityUser 并通过以下方式注册更改:

services.AddIdentity<AppUser, IdentityRole>(); //AppUser is our custom IdentityUser

现在,我们的Startup.cs 文件包含:

services.AddIdentity<AppUser, IdentityRole>();    
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"));

但是,服务解析器对此并不满意。

某些服务无法构建(验证时出错 服务描述符'ServiceType: Microsoft.AspNetCore.Identity.ISecurityStampValidator 生命周期:范围 实施类型: Microsoft.AspNetCore.Identity.SecurityStampValidator`1[GcfConnect.Application.Web.Security.Models.AppUser]': 无法解析类型的服务 'Microsoft.AspNetCore.Identity.IUserStore`1[GcfConnect.Application.Web.Security.Models.AppUser]' 尝试激活时...(SNIPPED)

如何在 Azure AD 身份验证中使用自定义 IdentityUser?我是否需要为整个身份系统(UserStoreUserManager 等)提供自定义替换?

【问题讨论】:

    标签: c# azure asp.net-core identity


    【解决方案1】:

    如果您使用的是EF,请尝试将其更改为运行。

    需要添加AddEntityFrameworkStores():

    services.AddIdentity<IdentityUser, IdentityRole>()
        .AddEntityFrameworkStores<WorldContext>();
    

    【讨论】:

    • 但我没有针对 EF 中的数据进行身份验证——只有在我拥有 Azure AD 的委托人之后,我才会手动查找其中的数据。 AddEntityFrameworkStores 仍然有意义吗?
    猜你喜欢
    • 2020-05-27
    • 2021-10-28
    • 1970-01-01
    • 2019-12-13
    • 2022-08-18
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    相关资源
    最近更新 更多