Simple Injector-值不能为null.参数名称:userManager



我正试图用Simple Injector安装ASP.NET Identity,但我遇到了一个又一个问题。我想我已经接近了,但现在我在说时出错了

值不能为null。

参数名称:userManager

描述:在执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误以及错误在代码中的来源的更多信息。

异常详细信息:System.ArgumentNullException:值不能为null。参数名称:userManager

[ArgumentNullException:值不能为null。参数名称:userManager]Microsoft.AspNet.Identity.Owin.SignInManager2..ctor(UserManager2 userManager,IAuthenticationManager authenticationManager)+81ILETSB.MCLETC.UI.ApplicationSignInManager.ctor(ApplicationUserManager userManager,IAuthenticationManager authenticationManager)在MCLETC.UI\App_Start\IdentityConfig.cs:42中MCLETC.UI.ApplicationSignInManager.Create(IdentityFactoryOptions1 options, IOwinContext context) in MCLETC.UIApp_StartIdentityConfig.cs:53 Microsoft.AspNet.Identity.Owin.IdentityFactoryProvider1.Create(Identity FactoryOptions`1选项,IOwinContext上下文)+14Microsoft.AspNet.Identity.Owin.d_5.MoveNext()+89System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)+102System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+64Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__7.MoveNext()+179System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)+102System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+64Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__12.MoveNext()+180Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar)+69Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPiplineContext.EndFinalWork(IAsyncResult ar)+64System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+389System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep步骤)+50System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,Boolean和completedSynchronously)+163

我在这里收到错误消息:

public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
: base(userManager, authenticationManager)
{
}
public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
{
return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
}
public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
{
return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
}
}

启动类别:

public partial class Startup
{
public void ConfigureAuth(IAppBuilder app, Container container)
{
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.CreatePerOwinContext(() => container.GetInstance<ApplicationUserManager>());
// Nothing modified below (template code)
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});            
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
}
}

SimpleInjectorInitializer:

public class SimpleInjectorInitializer
{
/// <summary>Initialize the container and register it as MVC5 Dependency Resolver.</summary>
public static Container Initialize(IAppBuilder app)
{
var container = GetInitializeContainer(app);
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
return container;
}
private static Container GetInitializeContainer(IAppBuilder app)
{
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
// IoC for ASP.NET Identity
container.RegisterInstance(app);
container.Register<ApplicationUserManager>(Lifestyle.Scoped);
container.Register(() => new ApplicationDbContext("Your constring goes here"), Lifestyle.Scoped);
container.Register<IUserStore<ApplicationUser>>(() => new UserStore<ApplicationUser>(container.GetInstance<ApplicationDbContext>()), Lifestyle.Scoped);
container.RegisterInitializer<ApplicationUserManager>(manager => InitializeUserManager(manager, app));
container.Register<SignInManager<ApplicationUser, string>, ApplicationSignInManager>(Lifestyle.Scoped);
container.Register(() => container.IsVerifying ? new OwinContext(new Dictionary<string, object>()).Authentication : HttpContext.Current.GetOwinContext().Authentication, Lifestyle.Scoped);
// Register all controllers
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
//Register Modules
BusinessModule.RegisterServices(container);
WebModule.RegisterServices(container);
return container;
}
private static void InitializeUserManager(ApplicationUserManager manager, IAppBuilder app)
{
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
//Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator()
{
RequiredLength = 8,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
manager.MaxFailedAccessAttemptsBeforeLockout = 3;
// Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
// You can write your own provider and plug it in here.
manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
{
MessageFormat = "Your security code is {0}"
});
manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
{
Subject = "Security Code",
BodyFormat = "Your security code is {0}"
});
manager.EmailService = new EmailService();
manager.SmsService = new SmsService();
var dataProtectionProvider = app.GetDataProtectionProvider();
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
}
}   

AccountControllerManageController的设置与此类似:

private SignInManager<ApplicationUser, string> _signInManager;
private ApplicationUserManager _userManager;
public AccountController(ApplicationUserManager userManager, SignInManager<ApplicationUser, string> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
} 

我为ApplicationUserManager注册了什么错误吗?

如果分析堆栈跟踪,问题应该会变得很清楚:

  • Microsoft.AspNet.Identity.Owin.SignInManager<TUser, TKey>的构造函数传递了userManager构造函数参数的null
  • 通过传递userManager参数,从应用程序的ILETSB.MCLETC.UI.ApplicationSignInManager的构造函数调用Microsoft.AspNet.Identity.Owin.SignInManager<TUser, TKey>构造函数
  • wILETSB.MCLETC.UI.ApplicationSignInManager的构造函数是从ApplicationSignInManager自己的Create方法中调用的

换句话说,静态ApplicationSignInManager.Create方法为ApplicationSignInManager构造函数提供了一个null值。

这就是分析可以走多远,因为您还没有提供Create方法的详细信息。

尝试通过放置断点来调试Create方法,以分析发生了什么以及为什么值为null

最新更新