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

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

问题描述

为我的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:没有注册默认实例
,并且无法自动确定类型
'IUserStore'

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 … }

查找丢失的替换项,右键单击
公共类ApplicationUser中的IdentityUser:IdentityUser

2)有时您进行编译,但并非所有内容都已更新,因此旧obj仍引用已更改的代码。使用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身份用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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