如何向IdentityUser添加额外信息并扩展UserManager方法

时间:2018-06-01 14:44:56

标签: c# asp.net asp.net-mvc asp.net-identity

我正在使用Asp.net mvc IdentityUser类在我的应用程序中存储用户。在数据库中有AspNetUsers表,所有用户都没有电子邮件。但现在我需要向ApplicationUser类添加额外的客户信息。现在,带有电子邮件的用户可以同时使用相同的电子邮件发送给客户A和客户B.

我的ApplicationUser类是这样的:

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    public Customer Customer { get; set; }
    [Required]
    public int CustomerId { get; set; }
}

但我无法找到扩展UserManager并根据需要更改方法的方法。

public async System.Threading.Tasks.Task<bool> AddAccount(string email, int customerId)
{
    // Asp.net User
    var user = await UserManager.FindByEmailAsync(email);
    string password = Membership.GeneratePassword(5, 1);
    if (user == null)
    {
        user = new ApplicationUser { UserName = email, Email = email, CustomerId = customerId };
        var result = await UserManager.CreateAsync(user, password);
        if (!result.Succeeded)
        {
            return false;
        }
    }

    return true;
}

我需要扩展FindByEmailAsync方法,它不仅需要使用电子邮件参数,还需要将CustomerId作为参数。

我需要FindByEmail(string email,int customerId),我不知道怎么做?

1 个答案:

答案 0 :(得分:0)

如果您想这样做,您必须创建继承自MyUserManager的{​​{1}}。然后添加方法UserManager

像这样:

FindByEmailAndId([your params])

但我不明白为什么?如果你创建一个用户,你的一个或两个参数必须是unic。那么为什么不使用这个参数?