【问题标题】:.NET identity user, separation of conserns.NET 身份用户,关注点分离
【发布时间】:2019-03-03 03:27:21
【问题描述】:

我正在尝试使用 .NET 身份。 我的用户具有与身份验证相关的属性。例如,电子邮件地址、密码、用户有权访问的项目等。 此外,用户还有其他字段,例如最喜欢的颜色。

  • 由于颜色与身份验证无关,我不想将颜色添加为 ApplicationUser 的属性:IdentityUser {}。

  • 但不知何故,我需要将值写入该字段,因此我可以编写一个存储库,它采用常规用户对象。但在这种情况下,我需要自己编写存储库,对密码进行哈希处理,那么拥有 .NET Identity 有什么意义呢?

  • 第三种解决方案是创建 2 个表,一个用于 ApplicationUser,一个用于相关属性,但我们已经有一个数据库,我不想更改所有内容。

  • 我能想到的另一个想法是让 ApplicationUser : IdentityUser {} 基本上是 User 的副本(我也不能从 user 和 IdentityUser 继承,也不能链接它,因为数据库首先)并在身份验证中有一个单独的 AuthenticationUser : IdentityUser 。上下文。

我是怎么做到的?

谢谢

【问题讨论】:

  • 您应该将其(最后一个解决方案)分开,因为与身份和其他数据相比,最喜欢的颜色将很少使用。从身份角度来看,用户有不同的使用上下文。
  • 请在此处阅读我的回答:stackoverflow.com/questions/52079466/…
  • @RuardvanElburg 这是一个很好的答案,我对此表示赞同并发表评论。但我的回答可能有用。

标签: asp.net authentication asp.net-identity identity


【解决方案1】:

我认为对于这个问题,你必须改变你的模型:

public class Person
{
    public string Color { get; set: }
    public int RoleId { get; set: }
    public Role Role { get; set: }

    public ICollection<UserAuthentication> UserAuthentications { get; set: }
}

public class UserAuthentication
{
    public Person Person { get; set: }
    public int PersonId { get; set: }
}

有了这些模型,你就没有这个问题了。

如果您有任何数据,您可以使用 TSql 替换它。

【讨论】:

    猜你喜欢
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多