C# 在用户登录时加载集合

C# 在用户登录时加载集合,c#,asp.net-mvc-5,asp.net-identity,C#,Asp.net Mvc 5,Asp.net Identity,好的,我已经定制了ApplicationUser,我需要在用户登录时加载一个集合,我只是想找到最好的方法。我看过web上的示例,但这些示例仅通过添加自定义属性(而不是集合)来自定义ApplicationUser。当用户登录时,我需要加载IList。我看到了几种方法可以做到这一点,我想知道哪种方法被认为是“最好的”方法(我知道这有点主观,但我认为这是一个新的领域) 无论如何,在AcccountController上的登录方法中,我们有: public async Task<Actio

好的,我已经定制了ApplicationUser,我需要在用户登录时加载一个集合,我只是想找到最好的方法。我看过web上的示例,但这些示例仅通过添加自定义属性(而不是集合)来自定义ApplicationUser。当用户登录时,我需要加载IList。我看到了几种方法可以做到这一点,我想知道哪种方法被认为是“最好的”方法(我知道这有点主观,但我认为这是一个新的领域)

无论如何,在AcccountController上的登录方法中,我们有:

    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
            var user = await UserManager.FindAsync(model.UserName, model.Password);
            if (user != null)
            {

                await SignInAsync(user, model.RememberMe);
            }
            else
            {
                ModelState.AddModelError("", "Invalid username or password.");
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }
并为我正在使用的自定义应用程序用户覆盖它,并加载我的属性。是这样做的,还是我忽略了
AspNet.Identity
中更简单的处理方法



因此,这很快就在我面前爆发了,似乎您不会继承
UserStore
,也不会访问重写其中一个方法时可能实际需要使用的任何内容,因为它们都是
internal
private
。除了复制和粘贴所有的
UserStore
和修改我想要的一种方法之外,还有什么建议吗?

这里有一种方法:

public class PatientPortalUserStore<TUser> : UserStore<TUser>
    where TUser : ApplicationUser
{

    public PatientPortalUserStore(DbContext context) : base(context)
    {
        this._userStore = new EntityStore<TUser>(context);
    }

    private EntityStore<TUser> _userStore;

    public override Task<TUser> FindByNameAsync(string userName)
    {
        //This is the important piece to loading your own collection on login
        IQueryable<TUser> entitySet =
            from u in this._userStore.EntitySet.Include(u=>u.MyCustomUserCollection)
            where u.UserName.ToUpper() == userName.ToUpper()
            select u;
        return Task.FromResult<TUser>(entitySet.FirstOrDefault<TUser>());
    }
}


//Had to define this because EntityStore used by UserStore<TUser> is internal
class EntityStore<TEntity>
    where TEntity : class
{
    public DbContext Context
    {
        get;
        private set;
    }

    public DbSet<TEntity> DbEntitySet
    {
        get;
        private set;
    }

    public IQueryable<TEntity> EntitySet
    {
        get
        {
            return this.DbEntitySet;
        }
    }

    public EntityStore(DbContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        this.Context = context;
        this.DbEntitySet = context.Set<TEntity>();
    }

    public void Create(TEntity entity)
    {
        this.DbEntitySet.Add(entity);
    }

    public void Delete(TEntity entity)
    {
        this.Context.Entry<TEntity>(entity).State = EntityState.Deleted;
    }

    public virtual Task<TEntity> GetByIdAsync(object id)
    {
        return this.DbEntitySet.FindAsync(new object[] { id });
    }
}
公共类PatientPortalUserStore:UserStore
其中TUser:ApplicationUser
{
公共PatientPortalUserStore(DbContext上下文):基础(上下文)
{
此._userStore=newentitystore(上下文);
}
私有实体商店_userStore;
公共覆盖任务FindByNameAsync(字符串用户名)
{
//这是登录时加载您自己的收藏的重要部分
易读实体集=
来自此中的u.\u userStore.EntitySet.Include(u=>u.MyCustomUserCollection)
其中u.UserName.ToUpper()==UserName.ToUpper()
选择u;
返回Task.FromResult(entitySet.FirstOrDefault());
}
}
//必须定义此项,因为UserStore使用的EntityStore是内部的
类实体库
地点:班级
{
公共数据库上下文
{
得到;
私人设置;
}
公共数据库集DbEntitySet
{
得到;
私人设置;
}
公共IQueryable实体集
{
得到
{
返回此.DbEntitySet;
}
}
公共实体存储(DbContext上下文)
{
if(上下文==null)
{
抛出新的ArgumentNullException(“上下文”);
}
this.Context=Context;
this.DbEntitySet=context.Set();
}
公共void创建(TEntity实体)
{
this.DbEntitySet.Add(实体);
}
公共作废删除(可撤销实体)
{
this.Context.Entry(entity.State=EntityState.Deleted;
}
公共虚拟任务GetByIdAsync(对象id)
{
返回此.DbEntitySet.FindAsync(新对象[]{id});
}
}
public class PatientPortalUserStore<TUser> : UserStore<TUser>
    where TUser : ApplicationUser
{

    public PatientPortalUserStore(DbContext context) : base(context)
    {
        this._userStore = new EntityStore<TUser>(context);
    }

    private EntityStore<TUser> _userStore;

    public override Task<TUser> FindByNameAsync(string userName)
    {
        //This is the important piece to loading your own collection on login
        IQueryable<TUser> entitySet =
            from u in this._userStore.EntitySet.Include(u=>u.MyCustomUserCollection)
            where u.UserName.ToUpper() == userName.ToUpper()
            select u;
        return Task.FromResult<TUser>(entitySet.FirstOrDefault<TUser>());
    }
}


//Had to define this because EntityStore used by UserStore<TUser> is internal
class EntityStore<TEntity>
    where TEntity : class
{
    public DbContext Context
    {
        get;
        private set;
    }

    public DbSet<TEntity> DbEntitySet
    {
        get;
        private set;
    }

    public IQueryable<TEntity> EntitySet
    {
        get
        {
            return this.DbEntitySet;
        }
    }

    public EntityStore(DbContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        this.Context = context;
        this.DbEntitySet = context.Set<TEntity>();
    }

    public void Create(TEntity entity)
    {
        this.DbEntitySet.Add(entity);
    }

    public void Delete(TEntity entity)
    {
        this.Context.Entry<TEntity>(entity).State = EntityState.Deleted;
    }

    public virtual Task<TEntity> GetByIdAsync(object id)
    {
        return this.DbEntitySet.FindAsync(new object[] { id });
    }
}