简体   繁体   English

Asp.Net身份-自定义数据库访问UserManager

[英]Asp.Net Identity - Custom Database Access UserManager

Is is possible to extend the UserStore/UserManager to have access to a custom table within an override method (without creating another dbcontext)? 是否可以扩展UserStore / UserManager以访问覆盖方法中的自定义表(而无需创建另一个dbcontext)?

I'm working on a legacy database which we integrated with Identity. 我正在使用与Identity集成的旧数据库。 Recently we added a new table, Clients, to the database that has a one to one relationship with the Users table. 最近,我们向数据库中添加了一个新表Clients,该表与Users表具有一对一的关系。 What would like to do is override the FindAsync method in the UserManager class to do call the base method, grab the user, and then based on the user's client Id stored in the table, fetch the client name and insert that into the user model. 要做的是重写UserManager类中的FindAsync方法,以调用基本方法,抓住用户,然后基于存储在表中的用户的客户端ID,获取客户端名称并将其插入用户模型。

The following is what I have attempted; 以下是我尝试过的; however, to me I think this is wrong approach and would like some suggestions on how to correctly apply what I'm trying to do. 但是,对我来说,我认为这是错误的方法,并希望就如何正确应用我要执行的操作提出一些建议。

public override async Task<ApplicationUser> FindAsync(string userName, string password)
{
    ApplicationUser user = await base.FindAsync(userName, password);

    if (user != null && user.ClientID != null)
    {
        using (var ctx = new ApplicationDbContext(HostPage.ConnectionString))
        {
            user.Client = ctx.Clients.Where(p => p.ClientID == user.ClientID).FirstOrDefault();
        }
    }

    return user;
}

你可以修改SQL base.FindAsync()执行加入到这样的表执行一个查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM