使用自定义用户而不是ASP.NET IdentityUser [英] Using custom user instead of ASP.NET IdentityUser

查看:150
本文介绍了使用自定义用户而不是ASP.NET IdentityUser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



ASP.NET Identity自动创建几个表格用于处理身份验证和授权本身。



我的主要目标是创建Users表,而其他。我尝试以下代码来阻止创建这些表:

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Ignore< IdentityUserRole>();
modelBuilder.Ignore< IdentityUserClaim>();
modelBuilder.Ignore< IdentityRole>();
}

当我想创建一个用户时,以下错误返回:



导航属性角色不是ApplicationUser类型上声明的属性。验证它没有被明确排除在模型中,并且它是一个有效的导航属性。



我发现内置的Identity用户具有以下结构:

  IdentityUser< string,IdentityUserLogin,IdentityUserRole,IdentityUserClaim> IUser,IUser< string> 

我需要创建一个不包含角色,声明等的自定义IdentityUser。我只想要表单用户,当我运行项目。



有可能创建我自己的IdentityUser或自定义内置的?或者任何建议只创建用户表并使用它?



非常感谢您的建议。

解决方案

这是为了获得解决方案所做的:




  • 我删除了base.OnModelCreating

  • 然后我按照这个顺序添加了忽略。

      // base .OnModelCreating(模型构建器); 
    modelBuilder.Entity< IdentityUser>()。ToTable(User);
    modelBuilder.Entity< IdentityUser>()。ToTable(User)忽略(p => p.Roles);
    modelBuilder.Ignore< IdentityUserRole>();
    modelBuilder.Ignore< IdentityUserLogin>();
    modelBuilder.Ignore< IdentityUserClaim>();
    modelBuilder.Ignore< IdentityRole>();




这样做我有一个最后的错误,它说:不能删除对象'dbo.Role',因为它被FOREIGN KEY约束引用。



为此,我更改了移动DropTable(dbo.Role )。我知道这是黑客,但我并没有发现EF迁移如何将droptables句子移动到正确的顺序。



Richard


I'm new on ASP.NET Identity and trying to customize the identity which is provided while creating new MVC project.

ASP.NET Identity automatically creates few tables for handle authentication and authorization itself.

My main goal is just create Users table but others. I've tried following code to prevent creating these tables:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Ignore<IdentityUserRole>();
        modelBuilder.Ignore<IdentityUserClaim>();
        modelBuilder.Ignore<IdentityRole>();
    }

When I want to create a user, following error returning:

The navigation property 'Roles' is not a declared property on type 'ApplicationUser'. Verify that it has not been explicitly excluded from the model and that it is a valid navigation property.

I found that built-in Identity user has following structure:

IdentityUser<string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>, IUser, IUser<string>

What I need is creating a custom IdentityUser which not contains role, claim, etc. I just want the table 'Users' when I run the project.

Any possibility to create my own IdentityUser or customize built-in one? Or any suggestion to create just 'Users' table and work with it?

Many thanks in advice.

解决方案

This is what I did in order to get a solution:

  • I removed the base.OnModelCreating
  • Then I added the ignore in this order.

    //base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<IdentityUser>().ToTable("User");
    modelBuilder.Entity<IdentityUser>().ToTable("User").Ignore(p => p.Roles);
    modelBuilder.Ignore<IdentityUserRole>();
    modelBuilder.Ignore<IdentityUserLogin>();
    modelBuilder.Ignore<IdentityUserClaim>();
    modelBuilder.Ignore<IdentityRole>();
    

Doing this I got a final error that it said: Could not drop object 'dbo.Role' because it is referenced by a FOREIGN KEY constraint.

For that I changed the migration moving the DropTable("dbo.Role"). I know this is hack, but I did not find out how EF migrations move the droptables sentences to the right order.

Richard

这篇关于使用自定义用户而不是ASP.NET IdentityUser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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