c# - 从基础 asp.net 身份用户创建继承用户

标签 c# asp.net entity-framework asp.net-mvc-4 asp.net-identity

我有一个问题,我想在这个例子中创建 N 个用户对象(例如客户和供应商),它们都是 asp.net IdentityUser 对象固有的。除了来自 IdentityUser 的数据之外,这些对象还有非常不同的附加数据。我想使用 IdentityUser 用户,因为这为我提供了一种灵活的方式来处理身份验证和授权。

这个例子已经非常精简,但应该提供关于无法创建具体用户(例如供应商的客户)的足够信息。看来我需要使用 UserManager 对象,因为它还负责创建例如密码哈希和其他安全信息。

我收到以下错误:

{“附加类型为“供应商”的实体失败,因为相同类型的另一个实体已经具有相同的主键值。这可能发生在使用“附加”方法或将实体状态设置为如果图中的任何实体具有冲突的键值,则为“未更改”或“已修改”。这可能是因为某些实体是新的并且尚未收到数据库生成的键值。在这种情况下,请使用“添加”方法或“已添加” ' 实体状态来跟踪图表,然后根据需要将非新实体的状态设置为“未更改”或“已修改”。”

IdentityUser 固有的类

 public class Customer : IdentityUser
 {
    public string CustomerProperty { get; set; }
 }

 public class Supplier : IdentityUser
 {
    public string SupplierProperty { get; set; }
 }

数据库上下文类

 public class ApplicationDbContext : IdentityDbContext {

      public ApplicationDbContext() : base("ApplicationDbContext")
      {
         Database.SetInitializer(new ApplicationDbInitializer());
      }

      public DbSet<Customer> CustomerCollection { get; set; }
      public DbSet<Supplier> SupplierCollection { get; set; }
 }

抛出异常的种子类

 public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
 {
    protected override void Seed(ApplicationDbContext context)
    {
        var userStore = new UserStore(context);
        var userManager = new UserManager(userStore);


        // Seed customer user which inherents from asp.net IdentityUser 
        var user = userManager.FindByEmail("customer@customer.com");
        if (user == null)
        {
            user = new User()
            {
                UserName = "customer@customer.com",
                Email = "customer@customer.com"
            };

            userManager.Create(user, userPassword);

            var customerUser = new Customer()
            {
                Id = user.Id,
                CustomerProperty = "Additional Info"
            };

            context.Entry(customerUser).State = EntityState.Modified;
            context.SaveChanges();
        }

        // Seed supplier user which inherents from asp.net IdentityUser 
        var user = userManager.FindByEmail("supplier@supplier.com");
        if (user == null)
        {
            user = new User()
            {
                UserName = "supplier@supplier.com",
                Email = "supplier@supplier.com"
            };

            userManager.Create(user, userPassword);

            var supplierUser = new Supplier()
            {
                Id = user.Id,
                IBAN = "212323424342234",
                Relationship = "OK"
            };

            context.Entry(supplierUser).State = EntityState.Modified;
            context.SaveChanges();
        }
    }
}

**** 更新 ****

下面的解决方案有效,但我仍在努力解决两个问题:

  1. 我总是希望有一种用户类型(例如,供应商的客户)与 IdentityUser 相关联。我考虑过使用界面,但这行不通。
  2. 如果我还在用户类型上添加对 IdentityUser 的虚拟引用,我会收到“无法确定‘ApplicaitonUser’和‘Supplier’类型之间关联的主体端”。该关联的主体端必须使用关系流畅的 API 或数据注释进行显式配置。异常(exception)。

 public class Customer 
 {
    [Key]
    public int CustomerId { get;set; }
    public string CustomerProperty { get; set; }

    *public virtual User User { get; set; }*

 }

 public class Supplier 
 {
    [Key]
    public int SupplierId { get;set; }
    public string SupplierProperty { get; set; }

    *public virtual User User { get; set; }*
 }

**类 IdentityUser(有效)**

public class User : IdentityUser
{
    public virtual Supplier Supplier { get; set; }
    public virtual Customer Customer { get; set; }
}

**类IdentityUser(我想要的)**

public class User : IdentityUser
{
    public virtual IConcreteUser ConcreteUser{ get; set; }
}

数据库上下文类

 public class ApplicationDbContext : IdentityDbContext {

      public ApplicationDbContext() : base("ApplicationDbContext")
      {
         Database.SetInitializer(new ApplicationDbInitializer());
      }

      public DbSet<Customer> CustomerCollection { get; set; }
      public DbSet<Supplier> SupplierCollection { get; set; }
 }

**播种类**

 public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
 {
protected override void Seed(ApplicationDbContext context)
{
    var userStore = new UserStore(context);
    var userManager = new UserManager(userStore);
    var roleManager = new RoleManager(roleStore);

    var user = userManager.FindByEmail("customer@customer.com");
    if (user == null)
    {
        user = new ApplicationUser()
        {
            UserName = "customer@customer.com",
            Email = "customer@customer.com"
            Customer = new Customer()
            {
                CustomerProperty = "Additional Info"
            }
        };

        userManager.Create(user, userPassword);
        roleManager.AddUserToRole("Customer");
    }

    user = userManager.FindByEmail("supplier@supplier.com");
    if (user == null)
    {
        user = new ApplicationUser()
        {
            UserName = "supplier@supplier.com",
            Email = "supplier@supplier.com",
            Supplier = new Supplier()
            {
                IBAN = "212323424342234",
                Relationship = "OK"
            }
        };

        userManager.Create(user, userPassword);
        roleManager.AddUserToRole("Supplier");
    }
}

最佳答案

和其他人一样,我认为这是一个设计问题。有一些替代方法,例如:

  1. 使用角色来定义“用户类型”(用户可以是供应商和客户)
  2. 使 SupplierCustomer 实体成为关系而不是用户的延伸

例如:

public class ApplicationUser : IdentityUser
{
    public virtual Customer Customer { get; set; }
    public virtual Supplier Supplier { get; set; }
}

public class Customer
{
    [Key]
    public int Id { get; set; }

    public virtual ApplicationUser User { get; set; }
    public string CustomerProperty { get; set; }
}

public class Supplier
{
    [Key]
    public int Id { get; set; }

    public virtual ApplicationUser User { get; set; }
    public string SupplierProperty { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<Customer> Customers { get; set; }
    public DbSet<Supplier> Suppliers { get; set; }
}

public class ApplicationDbInitializer
             : DropCreateDatabaseAlways<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        var userStore = new UserStore(context);
        var userManager = new UserManager(userStore);
        var roleManager = new RoleManager(roleStore);

        var user = userManager.FindByEmail("customer@customer.com");
        if (user == null)
        {
            user = new ApplicationUser()
            {
                UserName = "customer@customer.com",
                Email = "customer@customer.com"
                Customer = new Customer()
                {
                    CustomerProperty = "Additional Info"
                }
            };

            userManager.Create(user, userPassword);
            roleManager.AddUserToRole("Customer");
        }

        user = userManager.FindByEmail("supplier@supplier.com");
        if (user == null)
        {
            user = new ApplicationUser()
            {
                UserName = "supplier@supplier.com",
                Email = "supplier@supplier.com",
                Supplier = new Supplier()
                {
                    IBAN = "212323424342234",
                    Relationship = "OK"
                }
            };

            userManager.Create(user, userPassword);
            roleManager.AddUserToRole("Supplier");
        }
    }
}

按照您的逻辑,您可以执行以下操作:

if (User.IsInRole("Customer"))
{
    // do something
}

免责声明:这不是一个“复制和粘贴”示例,只是让您了解另一种方法。

关于c# - 从基础 asp.net 身份用户创建继承用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27059017/

相关文章:

c# - 尝试使用 Entity Framework 保存更改时出现异常

c# - 如何在 C#/.NET 5 中生成非源文件(例如 json)

c# - 类 VS 引用结构

C# WPF 应用程序在开发的计算机上运行,​​但不能在其他任何地方运行

asp.net - 用于验证数值的正则表达式

c# - Entity Framework 代码优先 : How to ignore classes

c# - 如何使用 Linq(聚合)折叠列表?

c# - 如何在 C# 代码后面编写这个 Javascript 代码?

c# - asp.net 中的文件 uploader 不显示文件

c# - 在 Multi-Tenancy .net 项目中实现角色的最佳方式是什么?