CustomUserStore的依赖注入,asp身份

时间:2016-09-27 11:00:46

标签: c# asp.net dependency-injection ninject

我从asp.net身份修改ApplicationUser类。现在来自ApplicationUser的Id字段具有int类型。

 public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim> 
{
    public ApplicationUser()
    {
        Posts = new List<Post>();
        Messages = new List<Message>();
        Friends = new List<ApplicationUser>();
    }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> 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 virtual Profile Profile { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
    public virtual ICollection<Message> Messages { get; set; }
    public virtual ICollection<ApplicationUser> Friends { get; set; }
}

public class CustomUserRole : IdentityUserRole<int> { }
public class CustomUserClaim : IdentityUserClaim<int> { }
public class CustomUserLogin : IdentityUserLogin<int> { }

public class CustomRole : IdentityRole<int, CustomUserRole>
{
    public CustomRole() { }
    public CustomRole(string name) { Name = name; }
}

public class CustomUserStore : UserStore<ApplicationUser, CustomRole, int,
    CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    public CustomUserStore(ApplicationDbContext context)
        : base(context)
    {
    }
}

public class CustomRoleStore : RoleStore<CustomRole, int, CustomUserRole>
{
    public CustomRoleStore(ApplicationDbContext context)
        : base(context)
    {
    }
} 
}

我想在我的服务中使用UserManager。现在我写这段代码

 public class MessageService : IMessageService
{
    private readonly IUnitOfWork _unitOfWork;
    private readonly UserManager<ApplicationUser, int> _manager;


    public MessageService(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
        _manager = new UserManager<ApplicationUser, int>(new CustomUserStore(_unitOfWork.GetContext()));
    }
}

但我想要这样的东西

 public class MessageService : IMessageService
{
    private readonly IUnitOfWork _unitOfWork;
    private readonly UserManager<ApplicationUser, int> _manager;


    public MessageService(IUnitOfWork unitOfWork, UserManager<ApplicationUser, int> manager)
    {
        _unitOfWork = unitOfWork;
        _manager = manager;
    }
}

我想使用Ninject来解决这个问题,但我现在不知道如何为我的自定义UserManager和USerStore绑定依赖项。有人能帮助我吗?

0 个答案:

没有答案