无法实现IUserStore

时间:2016-07-18 20:40:10

标签: asp.net-core asp.net-identity

我想使用(_ >: Person) with Swim,但没有Microsoft.AspNetCore.Identity - 我正在尝试使用自己的EntityFramework实现。我还没有决定使用什么数据库,但我认为这对于这个问题并不重要。

相关课程,尽可能剥离:

Startup.cs:

IUserStore

ApplicationUser:

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddUserStore<CustomUserStore<ApplicationUser>>()
            .AddUserManager<CustomUserManager>();
    }

CustomUserStore:

public class ApplicationUser
{
    public string UserName { get; set; }
    public string Email { get; set; }
}

CustomUserManager:

public class CustomUserStore<TUser> : IUserStore<TUser> where TUser : ApplicationUser
{
    private bool _disposed;

    public async Task<IdentityResult> CreateAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
    {
        //Trying to hit a breakpoint here
        throw new NotImplementedException();
    }

    //Other method implementations removed for brevity

    void IDisposable.Dispose()
    {
        _disposed = true;
    }
}

ExampleController:

 public class CustomUserManager : UserManager<ApplicationUser>
{
    public CustomUserManager(IUserStore<ApplicationUser> store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<ApplicationUser> passwordHasher, IEnumerable<IUserValidator<ApplicationUser>> userValidators, IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<ApplicationUser>> logger)
        : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors,services,logger)
    {

    }

    public override async Task<IdentityResult> CreateAsync(ApplicationUser user)
    {
        // Trying to hit a breakpoint here
        return await this.Store.CreateAsync(user, new CancellationToken());
    }
}

当我点击我用来测试它的网址[Route("api/example")] public class ExampleController : Controller { private CustomUserManager _userManager; public ExampleController(CustomUserManager userManager) { _userManager = userManager; } [Route("test")] public void Test() { var user = new ApplicationUser { UserName = "something", Email = "somthingElse" }; _userManager.CreateAsync(user, "password"); } } 时,api/example/test中的构造函数被点击,CustomUserManager中的CreateAsync被点击,因此CustomUserManager没有任何内容。

1 个答案:

答案 0 :(得分:1)

您正在覆盖CreateAsync(ApplicationUser user)中的CustomUserManager方法,但在Test()方法中,您正在调用CreateAsync(ApplicationUser user, string password)方法。

你正在调用错误的方法。