我在带有 Identity 2 的 MVC 5 项目中使用 Ninject。
对于其余的数据上下文和控制器,我对依赖注入没有任何问题。
对于使用 Identity 2 模型的帐户控制器,当我尝试登录时,我得到 null UserManager:
public class AccountController : Controller
{
private ApplicationUserManager _userManager;
public AccountController()
{
}
public AccountController(ApplicationUserManager userManager)
{
UserManager = userManager;
}
public ApplicationUserManager UserManager {
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
注入所需依赖项的正确方法是什么?我没有创建自定义 UserManager,它是开箱即用的模型,ApplicationUserManager 在 App_Start 下的 IdentityConfig.cs 中定义。
附带说明:我正在使用 Ninject 的约定扩展:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind(
x => x.FromThisAssembly()
.SelectAllClasses()
.BindAllInterfaces()
);
Thanks.
我在使用 Ninject4.0、MVC5.2.3、Identity2.0 和 Owin3.0.1 时遇到了同样的问题,在这里你应该做的是让一切正常工作。
-
首先你应该得到Ninject.Web.Common.OwinHost来自 NuGet,因为如果您使用 Owin,则需要使用 NinjectMiddleware。
public partial class StartUp
{
private IKernel kernel = null;
public void Configuration(IAppBuilder app)
{
kernel = CreateKernel();
app.UseNinjectMiddleware(() => kernel);
ConfigureAuth(app);
}
public IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
// TODO: Put any other injection which are required.
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
}
-
正如你所看到的,我们有一个方法可以注入我们想要的所有服务,在这种情况下我们必须注入应用程序用户管理器, 用户存储库 and 数据库上下文。因此对于注册服务 in Startup类,我们有:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<DbContext>().To<ApplicationDbContext>();
kernel.Bind<IUserStore<User, long>>().To<UserStore<User, Role, long, UserLogin, UserRole, UserClaim>>();
kernel.Bind<ApplicationUserManager>().ToSelf();
}
注意:我使用 long 而不是 string 作为 Identity 上所有键的类型,但是
使用默认值(字符串作为键)应该是相同的。
-
In StartUp.Auth.cs文件包含部分StartUp我们应该改变的班级配置验证方法:
internal static IDataProtectionProvider DataProtectionProvider
{ get; private set; }
public void ConfigureAuth(IAppBuilder app)
{
DataProtectionProvider = app.GetDataProtectionProvider();
//app.CreatePerOwinContext<ApplicationUserManager(ApplicationUserManager.Create)
app.CreatePerOwinContext<ApplicationUserManager>(
(option, context) => {
var kernl = context.Get<IKernel>();
return kernel.Get<ApplicationUserManager>();
});
//rest of the code
}
注:我们得到应用程序用户管理器 from owin before but now我们从Ninject内核.请注意,我们创建了一个内部静态属性数据保护提供者并从 appBuilder 设置它,我们将在身份配置/应用程序用户管理器.(稍后见)
-
现在我们必须改变应用程序用户管理器,我们从中删除代码Create方法构造函数只有一个依赖项用户存储库,这是最终的外观:
public class ApplicationUserManager : UserManager<User,long>
{
public ApplicationUserManager(IUserStore<User,long> store)
:base(store)
{
this.UserValidator = new UserValidator<User, long>(this)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
this.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
var dataProtectionProvider = Startup.DataProtectionProvider;
// this is unchanged
if (dataProtectionProvider != null)
{
IDataProtector dataProtector =
dataProtectionProvider.Create("ASP.NET Identity");
this.UserTokenProvider =
new DataProtectorTokenProvider<User,long>(dataProtector);
}
}
}
-
最后我们可以使用应用程序用户管理器 in our 账户控制器的 构造函数像这样:
public class AccountController : ApiController
{
private ApplicationUserManager UserManager ;
public AccountController(ApplicationUserManager userManager){
UserManager = userManager;
}
// all apis come here
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)