如何从ASP Identity 2.2中的扩展标识,AspUser缓存和获取属性

时间:2017-02-25 05:35:03

标签: asp.net-mvc entity-framework session asp.net-identity

为了支持天安/公司,我在ASP MVC 5,Identity 2.2角色管理中添加/扩展了一个新属性到名为OrgId Login(),我添加了相应的或者对其他一些表格looked here but no answers

在用户UserSessionApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());

期间
  1. 如何缓存,配置&amp;检索OrgId,以便我可以为组织特定记录执行DBConext过滤/ CRUD
  2. 建议:最好将其保存在Claims,FormsToken或Session中 如何在会话中设置tenanId上下文?
  3. 我知道如何获得用户,但不知道扩展的组织

    {{1}}

2 个答案:

答案 0 :(得分:1)

您的自定义用户类应如下所示:

public class CustomizedUser : IdentityUser
    {
        public int OrgId {get; set;}
        public DateTime JoinDate { get; set; }
        //...
        // and other simple fields

       //Fields related to other tables
        public virtual ICollection<Article> Articles { get; set; } = new List<Article>();
      //...      
}

您的CustomizedApplicationDbContext类

public class CustomizedApplicationDbContext : IdentityDbContext<CustomizedUser>, IApplicationDbContext
{
    public CustomizedApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static CustomizedApplicationDbContext Create()
    {
        return new CustomizedApplicationDbContext();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //your entity configurations go here if you have any

        base.OnModelCreating(modelBuilder);
    }

    //These fields are neccessary in order to maintain the power of base class
    public DbChangeTracker Changetracker => base.ChangeTracker;
    public Database DatabBase => base.Database;

    //your own dbsets go here except CustomizedUser, because the base class IdentityDbContext<CustomizedUser> handles it
    public IDbSet<Article> Articles { get; set; }
    //...

}

现在,请记住用CustomizedApplicationDbContext替换每个ApplicationDbContext引用,并在代码中使用CustomizedUser引用每个IdentityUser引用(特别是在mvc创建的ManageController中)。

从现在开始,您可以像其他表一样轻松访问用户表:

var db = new CustomizedApplicationDbContext();
var user = db.CustomizedUsers.First(x=> x.OrgId == 10 || Email == "sth@yahoo.com");

要获得当前用户OrgId,您可以使用类似的东西(不查询数据库):

var currentUserOrgId = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(User.Identity.GetUserId()).OrgId;

希望这是有帮助的

答案 1 :(得分:1)

您可以在ASP.NET Identity中获取当前用户,如下所示:

ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext()
    .GetUserManager<ApplicationUserManager>()
    .FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());

//If you use int instead of string for primary key, use this:
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext()
    .GetUserManager<ApplicationUserManager>()
    .FindById(Convert.ToInt32(System.Web.HttpContext.Current.User.Identity.GetUserId()));


AspNetUsers表中获取自定义属性:

ApplicationUser user = UserManager.FindByName(userName);
string name = user.Name;

希望这会有所帮助......