从基本asp.net用户身份创建继承用户 [英] Creating inheritance users from base asp.net identity user
问题描述
我有问题,我想在这个例子创建N,二,用户对象(例如客户和放大器;供应商),所有从asp.net IdentityUser对象所固有的。这些对象另外还有从IdentityUser的数据非常不同的附加数据。我想用IdentityUser用户,因为这给了我服用认证和授权的护理的灵活方式。
I have problem in which i would like to create N, two in the example, user objects (e.g. Customer & Supplier) which all inherent from the asp.net IdentityUser object. These object have very different additional data besides the the data from the IdentityUser. I would like to use the IdentityUser user as this gives me a flexible way of taking care of authentication and authorization.
这个例子已经非常精简,但应提供有关不能够创建一个具体的用户(例如供应商的客户)的足够信息。看来我需要使用的UserManager对象,因为这也需要创建例如密码哈希和额外的安全性信息的服务。
This example has been very stripped down but should supply sufficient information concerning the not being able to create a concrete user (e.g. Customer of Supplier). It seems i need to use the UserManager object as this also takes care of creating for example the password hash and additional security information.
我得到presented以下错误:
I get presented the following error:
的 {附加型供应商的实体失败,因为同类型的另一实体已经有相同的主键值。发生这种情况用'附加'方法或设置实体的状态时, '不变'或'修改',如果图中的任何实体有冲突的键值。这可能是因为一些实体是新的,但尚未收到数据库生成的键值。在这种情况下使用添加方法或加实体状态追踪图形,然后将非新实体的状态为未改变或修饰的适当。} 的
类从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();
}
}
}
**** ****更新
**** UPDATE ****
以下作品中的解决方案,但我仍然有两个问题挣扎:
The solution below works but i am still struggling with two issues:
- 我总是希望有一个用户类型(例如,供应商的客户)与IdentityUser有关。我虽然有关使用一个接口,但不起作用。
- 如果我还对IdentityUser添加用户的类型我得到一个无法确定类型之间的关联的主要结束ApplicaitonUser和供应商虚拟参考。该协会的主要终点必须用的关系,流利的API或数据注解显式地配置。例外。
类
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(工作)**
**Class IdentityUser (which works) **
public class User : IdentityUser
{
public virtual Supplier Supplier { get; set; }
public virtual Customer Customer { get; set; }
}
**类IdentityUser(我想什么)**
**Class IdentityUser (what i would like) **
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; }
}
**播种类**
**Seeding class **
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");
}
}
}
推荐答案
正如其他人也这样做,我认为这是一个设计问题。还有像一些替代方法:
As others do too I think this is a design problem. There are some alternative approaches like:
- 使用角色来定义用户类型(用户可供应商和客户)
- 请在
公司
和客户
实体的关系不扩展用户 的
- use roles to define the "user-type" (a user can be supplier AND customer)
- make the
Supplier
andCustomer
entities a relation not extension of the user
例如:
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");
}
}
}
和在你的逻辑,你可以这样做:
and in your logic you can do something like:
if (User.IsInRole("Customer"))
{
// do something
}
的免责声明的:这不是一个复制和放大器;粘贴的例子,应该只给你一个不同的方法的想法
DISCLAIMER: This is not a "copy&paste" example and should just give you an idea of a different approach.
这篇关于从基本asp.net用户身份创建继承用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!