扩展 ASP.NET Core Identity 用户 [英] extend ASP.NET Core Identity user

查看:89
本文介绍了扩展 ASP.NET Core Identity 用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为我的 DI 使用结构图.默认情况下一切正常,但是我想将 fn 和 ln 添加到 AspNetUser DB.我添加了一个新类,如下所示:

Using structuremap for my DI. By default everything works, however I want to add fn and ln to AspNetUser DB. I've added a new class like so:

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

修改的Startup.cs:

modified Startup.cs:

    services.AddDbContext<AppDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<AppDbContext>()
        .AddDefaultTokenProviders();

修改后的 ApplicationDbContext:

Modified ApplicationDbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}

控制器(从 Identityuser 修改为 AppliationUser):

Controller (modified to AppliationUser from Identityuser):

    public AccountController(
        SignInManager<ApplicationUser> signInManager,
        UserManager<ApplicationUser> userManager)
    {
        m_signInManager = signInManager;
        m_userManager = userManager;
    }

我遇到的错误:

StructureMapConfigurationException: 没有注册默认实例并且不能自动确定类型'用户商店'

StructureMapConfigurationException: No default Instance is registered and cannot be automatically determined for type 'IUserStore'

没有为 IUserStore 指定配置

There is no configuration specified for IUserStore

推荐答案

致 Striter:

在创建子类时很容易错过对身份类的更新.

Is easy to miss an update to identity classes when creating subclases.

所以,这个想法是:

1) 检查您将从身份覆盖的基类:ApplicationUser、ApplicationRole、(如果您修改它,不是很常见)、ApplicationUserLogin、ApplicationClaims 等.请注意,我的示例取自我修改 UserRoles 以包含公司的代码,因此声明有点复杂.

1) Check base clases you will override from identity: ApplicationUser, ApplicationRole, (if you modify it, not very common), ApplicationUserLogin, ApplicationClaims… etc. Note my example is taken from code I have where I modified UserRoles to include a company, so it's a bit more complex on the declaration.

例如,您可能有:

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{ // Code… }

或(未测试)

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, IdentityUserRole<int>, IdentityUserClaim<int>>
{ // Code… }

然后在所有子程序中替换它们.对于 ShaneKm,他可能在 DbContext 或 UserStore 上保持不变:

Then replace them accord in all subroutines. In case of ShaneKm, he probably left that unchanged on DbContext or UserStore:

public class ApplicationDbContext : IdentityDbContext<IdentityUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{ // Code … }

并修复它:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{ // Code … }

要搜索丢失的替换,请右键单击IdentityUser"公共类 ApplicationUser : IdentityUser

2) 有时候你编译了,并不是所有的东西都更新了,所以旧的 objs 仍然指的是已经改变的代码.WCF经常发生在我身上.您必须清理 obj 和 bin 目录(我将它们删除)以确保所有内容都重新编译.

Fo search of missing replacements, right-click on "IdentityUser" in public class ApplicationUser : IdentityUser

2) Sometimes you compile and not all things are updated, so old objs still refers to already changed code. Happens to me frequently with WCF. You have to clean obj and bin directories (I delete them) to ensure everything gets recompiled.

希望这能澄清.

这篇关于扩展 ASP.NET Core Identity 用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆