【问题标题】:Using ApplicationUser in MVC application在 MVC 应用程序中使用 ApplicationUser
【发布时间】:2017-05-26 08:02:57
【问题描述】:
谁能告诉我一些关于在 MVC 应用程序中使用 ApplicationUser 的最佳实践的文档?
我有一些基于 ApplicationUser 类的类(例如,一个“Employee”类供内部人员使用该应用程序和一个“Client”类也为客户提供一些访问权限。
似乎我应该从 ApplicationUser 继承或将其作为外键引用 - 但我不确定 a)如何执行此操作或 b)正确的方法是什么。
(我使用 Code First Entity Framework 来搭建类。)
【问题讨论】:
标签:
c#
entity-framework
model-view-controller
【解决方案1】:
我在编写带有基于令牌的授权的 REST API 时使用了this。它还包含实现电子邮件服务、刷新令牌服务等的良好实践。有 6 篇文章专注于身份验证。一旦我不得不将项目从 MySql Membership Authorization 迁移到 Microsoft Identity,我将其用作reference。在架构方面(例如 CoreModels -> 业务逻辑服务/提供者 -> UI 提供者)我有一个单独的项目,其中授权模块有它的主要组件,例如
- 用户管理器
- 角色管理器
- 登录管理器
- 角色存储
- 用户存储
以及提供者(如果您想使用基于声明的授权或 OAuth,您将需要它们)
我建议你继承 ApplicationUser 类并以任何你喜欢的方式扩展它
public partial class User : IdentityUser<Guid, UserLogin, UserRole, UserClaim>
{
public decimal Balance { get; set; }
public string NickName { get; set; }
public int AnotherEntityId { get; set; }
[ForeignKey("AnotherEntityId ")]
public virtual AnotherEntity AnotherEntity { get; set; }
public virtual ICollection<OtherEntity> OtherEntities { get; set; }
public User()
{
Id = Guid.NewGuid();
}
}
而不是有 2 个包含几乎相似的用户信息的表。角色将为您提供访问限制功能(如果客户和员工应该具有不同的访问级别)