繁体   English   中英

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

[英]How to add extra information to IdentityUser and extend UserManager methods

我正在使用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(电子邮件字符串,int customerId),我不知道该怎么做?

如果要执行此操作,则必须创建从UserManager继承的MyUserManager 然后添加方法FindByEmailAndId([your params])

像这样:

public class MyUserManager<TUser> : UserManager<TUser>
{
    public async Task<TUser> FindByEmailAsync(string email, int customerId)
    {
        //your code.
    }
}

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

暂无
暂无

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

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