2016-04-27 4 views
0

Asp.NET ID 3はUserManagerの引数の1つのコンストラクタを削除したので、UserManagerから継承する場合は10個の引数をすべて指定する必要があります。私は、コントローラを呼び出すときので、これは動作しないと仮定しASP.NET IDを使用するカスタムパスワードポリシー3

public class AppUserManager : UserManager<ApplicationUser> 
{ 
    public AppUserManager() : base(new UserStore<ApplicationUser>(new AppDbContext()), null, null, null, new PasswordValidator[] { new PasswordValidator() }, null, null, null, null, null) 
    { 

    } 
} 

public class PasswordValidator : IPasswordValidator<ApplicationUser> 
{ 
    public Task<IdentityResult> ValidateAsync(UserManager<ApplicationUser> manager, ApplicationUser user, string password) 
    { 
     return Task.Run(() => 
     { 
      if (password.Length >= 4) return IdentityResult.Success; 
      else { return IdentityResult.Failed(new IdentityError { Code = "SHORTPASSWORD", Description = "Password too short" }); } 
     }); 
    } 
} 

[HttpPost] 
    public async Task<dynamic> Post([FromBody] RegisterSchema req) 
    { 
     if (ModelState.IsValid) 
     { 
      var user = new ApplicationUser(req.username); 
      AppUserManager um = new AppUserManager(); 
      var result = await um.CreateAsync(user, req.password); 
      return result; 
     } 

resultは常にnullである(umながら、簡単な作業で、私はachiveしたので、私はこのコードを書きました

答えて

1

これは、コンストラクタに非常に多くのnullを入れている可能性があります。 UserManagerの上書きに使用するコンストラクタは次のとおりです。

using System; 
using System.Collections.Generic; 
using System.Threading.Tasks; 
using Microsoft.AspNet.Http; 
using Microsoft.AspNet.Identity; 
using Microsoft.Extensions.Logging; 
using Microsoft.Extensions.OptionsModel; 

namespace [YourApp].Services 
{ 
    public class ApplicationUserManager : UserManager<ApplicationUser> 
    { 
     public ApplicationUserManager(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, 
             IHttpContextAccessor contextAccessor) 
     : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger, contextAccessor) 
     { 

     } 
    } 
} 
+0

はい、あなたのコードはokです。とにかく私は同様の方法で問題を解決しましたが、より多くのコードを(ちょっとした柔軟性のために) – Cristiano

+0

upvoteを取得する可能性はありますか?それは丁寧です。私は賛成を返すでしょう。 :-) –

1

これまでの回答はうまくいきます。 )(あなたはあなたのConfigureServicesでサービスを注入することができる

public class AppUserManager : UserManager<ApplicationUser> 
{ 
    public AppUserManager(IServiceProvider services, IHttpContextAccessor contextAccessor, ILogger<UserManager<ApplicationUser>> logger) : base(new UserStore<ApplicationUser>(new ApplicationDbContext()), new CustomOptions(), new PasswordHasher<ApplicationUser>(), new UserValidator<ApplicationUser>[] { new UserValidator<ApplicationUser>() }, new PasswordValidator[] { new PasswordValidator() }, new UpperInvariantLookupNormalizer(), new IdentityErrorDescriber(), services, logger, contextAccessor) 
    { 
    } 
} 

public class PasswordValidator : IPasswordValidator<ApplicationUser> 
{ 
    public Task<IdentityResult> ValidateAsync(UserManager<ApplicationUser> manager, ApplicationUser user, string password) 
    { 
     return Task.Run(() => 
     { 
      if (password.Length >= 4) return IdentityResult.Success; 
      else { return IdentityResult.Failed(new IdentityError { Code = "SHORTPASSWORD", Description = "Password too short" }); } 
     }); 
    } 
} 

public class CustomOptions : IOptions<IdentityOptions> 
{ 
    public IdentityOptions Value { get; private set; } 
    public CustomOptions() 
    { 
     Value = new IdentityOptions 
     { 
      ClaimsIdentity = new ClaimsIdentityOptions(), 
      Cookies = new IdentityCookieOptions(), 
      Lockout = new LockoutOptions(), 
      Password = null, 
      User = new UserOptions(), 
      SignIn = new SignInOptions(), 
      Tokens = new TokenOptions() 
     }; 
    }  
} 

:私は完全なモデルを必要とする人のための私のバージョンを添付

 services.AddScoped<AppUserManager>(); 
関連する問題