为了准确回答我的问题,这里是代码和说明:
步骤1:
创建自定义用户存储。
public class ApplicationUserStore : UserStore<ApplicationUser>
{
public ApplicationUserStore(ApplicationDbContext context)
: base(context)
{
}
}
第二步:
更新ApplicationUserManager并将代码从Create方法移动到构造函数中
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store, IdentityFactoryOptions<ApplicationUserManager> options)
: base(store)
{
this.UserValidator = new UserValidator<ApplicationUser>(this)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
this.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
this.UserLockoutEnabledByDefault = true;
this.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
this.MaxFailedAccessAttemptsBeforeLockout = 5;
this.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
{
MessageFormat = "Your security code is {0}"
});
this.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
{
Subject = "Security Code",
BodyFormat = "Your security code is {0}"
});
this.EmailService = new EmailService();
this.SmsService = new SmsService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
this.UserTokenProvider =
new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
}
}
第三步:
修改Startup.Auth类,并注释掉以下代码
第四步:
更新帐户控制器(或其他相关控制器),并添加以下构造函数
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, IAuthenticationManager authManager)
{
_userManager = userManager;
_signInManager = signInManager;
_authManager = authManager;
}
步骤5:更新账户控制器,使属性仅可检索为:
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager;
}
}
public ApplicationUserManager UserManager
{
get
{
return _userManager;
}
}
private IAuthenticationManager AuthenticationManager
{
get
{
return _authManager;
}
}
步骤6:更新Startup.cs文件
public partial class Startup
{
private IAppBuilder _app;
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
_app = app;
app.UseNinjectMiddleware(CreateKernel);
}
private IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
kernel.Bind<ApplicationDbContext>().ToSelf().InRequestScope();
kernel.Bind<IUserStore<ApplicationUser>>().To<ApplicationUserStore>();
kernel.Bind<ApplicationUserManager>().ToSelf();
kernel.Bind<ApplicationSignInManager>().ToSelf();
kernel.Bind<IAuthenticationManager>().ToMethod(x => HttpContext.Current.GetOwinContext().Authentication);
kernel.Bind<IDataProtectionProvider>().ToMethod(x => _app.GetDataProtectionProvider());
return kernel;
}
}
为了进一步回答这个问题,基于我所收到的评论:
这些管理器不应该被注入为类,因为这样你就无法完成 DI。相反,应该创建多个接口,根据需要进一步分离和组织 UserManager 的方法。以下是一个示例:
public interface IUserManagerSegment
{
Task<IdentityResult> CreateAsync(ApplicationUser user, string password);
Task<IdentityResult> CreateAsync(ApplicationUser user);
Task<IdentityResult> ConfirmEmailAsync(string userId, string token);
Task<ApplicationUser> FindByNameAsync(string userName);
Task<bool> IsEmailConfirmedAsync(string userId);
Task<IdentityResult> ResetPasswordAsync(string userId, string token, string newPassword);
Task<IList<string>> GetValidTwoFactorProvidersAsync(string userId);
Task<IdentityResult> AddLoginAsync(string userId, UserLoginInfo login);
void Dispose(bool disposing);
void Dispose();
}
以上方法是我选择的一些随机方法,只是为了说明这一点。 话虽如此,现在我们将根据接口注入方法,例如:
kernel.Bind<IUserManagerSegment>().To<ApplicationUserManager>();
现在我们的AccountController构造函数看起来像这样:
public AccountController(IUserManagerSegment userManager, ApplicationSignInManager signInManager, IAuthenticationManager authManager)
{
_userManager = userManager;
_signInManager = signInManager;
_authManager = authManager;
}
SignInManager和AuthenticationManager也应该采取同样的措施。
以上代码已经过测试并且可以正常工作。请确保您已引用以下DLL文件:
Ninject.dll
Ninject.Web.Common
Ninject.Web.Common.OwinHost
Ninject.Web.Mvc
kernel.Bind<ApplicationUserManager>().ToMethod(ctx => ApplicationUserManager.Create()).InRequestScope()
(等等)吗? - BatteryBackupUnitUseNinjectMiddleware
时,它只能与ApiControllers一起使用。如果您想使其与常规控制器一起工作,则需要使用NinjectWebCommon
类的RegisterServices
方法,该方法将在使用Ninject.MVC5包的情况下添加到您的项目中。 - nemesv