UserManager在ASPNET Identity 2应用程序中始终为null

时间:2014-10-14 10:05:14

标签: asp.net-mvc-5 asp.net-identity-2

设定:

我有一个MVC 5应用程序,其中包含许多库项目,使用我自己的导出模板创建。导出的模板工作正常。

我正在使用ASPNET身份。我只是使用相关NuGet包中提供的Microsoft Aspnet Identity Sample的副本,我已编织到导出的模板中。这一直很好。

我没有触及ASPNET Identity 2示例中提供的文件。

IdentityConfig.cs文件中发生错误。

出于某种原因,它开始出现一个错误,指出它无法加载System.Web.Mvc的文件,因为它找不到版本5.1.0.0。

因此,我使用NuGet更新了Microsoft.Aspnet.Mvc包。这安装了5.2.2.0版本的system.web.mvc,这有效地清除了该错误。

...然而

虽然应用程序加载,但每当我尝试登录或创建新用户时,都会出现一个新错误(如下所示),主要表明ASPNET Identity UserManager对象为空。

我更新了microsoft.aspnet.identity包,但是在尝试登录或创建新用户时仍然会出现错误(登录页面显示正常,但是当您单击登录按钮时会发生错误)

在收到有关system.web.mvc参考的错误之前,我可以在闲暇时登录并注册用户。

错误:

这是我尝试登录时显示的错误。当我尝试注册一个新用户时,我得到一个不同的错误,但原因相同:UserManager对象为null,当它不应该是。

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 324:        public async Task<SignInStatus> PasswordSignIn(string userName, string password, bool isPersistent, bool shouldLockout)
Line 325:        {
Line 326:            var user = await UserManager.FindByNameAsync(userName);
Line 327:            if (user == null)
Line 328:            {

Source File: c:\Users\[user name]\Documents\Visual Studio 2013\Projects\[My solution]\Models\IdentityConfig.cs    Line: 326 

问题:

  1. 有谁知道造成这种情况的原因是什么?

  2. 例如,有可能Microsoft Aspnet Identity示例代码需要更新system.web.mvc dll的5.2.2.0版本吗?

  3. 注意:我担心在错误发生之前我无法确定或回想起我改变了什么。我有一段时间没有在这个项目上工作。

1 个答案:

答案 0 :(得分:22)

经过多次痛苦,我找到了答案:

由于某种原因,启动文件(〜/ App_Startup / Startup.Auth.cs),本来应该包含配置owin的代码,但没有。不确定这是怎么发生的。

所以我从Microsoft.Aspnet.Identity.Samples代码中复制了相应的文件,现在它可以工作了。代码是:

public partial class Startup
    {
        // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
        public void ConfigureAuth(IAppBuilder app)
        {
            // Configure the db context, user manager and role manager to use a single instance per request
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

            // Enable the application to use a cookie to store information for the signed in user
            // and to use a cookie to temporarily store information about a user logging in with a third party login provider
            // Configure the sign in cookie
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {
                    // Enables the application to validate the security stamp when the user logs in.
                    // This is a security feature which is used when you change a password or add an external login to your account.  
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                }
            });
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

            // Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
            app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));

            // Enables the application to remember the second login verification factor such as phone or email.
            // Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
            // This is similar to the RememberMe option when you log in.
            app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);

            // Uncomment the following lines to enable logging in with third party login providers
            //app.UseMicrosoftAccountAuthentication(
            //    clientId: "",
            //    clientSecret: "");

            //app.UseTwitterAuthentication(
            //   consumerKey: "",
            //   consumerSecret: "");

            //app.UseFacebookAuthentication(
            //   appId: "",
            //   appSecret: "");

            //app.UseGoogleAuthentication(
            //    clientId: "",
            //    clientSecret: "");
        }
    }

我以前一直在使用代码,但确实在某一时刻删除了owin包。我没有手动触摸文件,所以仍然不确定这是怎么发生的。