Asp.net mvc 5 ASPNET Identity 2应用程序中的UserManager始终为空 设置:

Asp.net mvc 5 ASPNET Identity 2应用程序中的UserManager始终为空 设置:,asp.net-mvc-5,asp.net-identity-2,Asp.net Mvc 5,Asp.net Identity 2,我有一个MVC5应用程序,其中包含许多库项目,使用我自己导出的模板创建。导出的模板工作正常 我正在使用ASPNET标识。我只是在使用相关NuGet软件包中提供的Microsoft Aspnet Identity示例的副本,该副本已编织到导出的模板中。这一直运作良好 我没有接触过ASPNET Identity 2示例中提供的文件 该错误发生在IdentityConfig.cs文件中 出于某种原因,它开始出现一个错误,指出它无法加载System.Web.Mvc的文件,因为它找不到版本5.1.0.0

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

我正在使用ASPNET标识。我只是在使用相关NuGet软件包中提供的Microsoft Aspnet Identity示例的副本,该副本已编织到导出的模板中。这一直运作良好

我没有接触过ASPNET Identity 2示例中提供的文件

该错误发生在IdentityConfig.cs文件中

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

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

然而

尽管应用程序已加载,但每当我尝试登录或创建新用户时,都会出现一个新错误(如下所示),基本上表明ASPNET Identity UserManager对象为null

我更新了microsoft.aspnet.identity软件包,但在尝试登录或创建新用户时仍会发生错误(登录页面显示“确定”,但单击“登录”按钮时会发生错误)

在得到有关system.web.mvc引用的错误之前,我可以随时登录并注册用户

错误: 这是我尝试登录时显示的错误。当我尝试注册一个新用户时,我得到一个不同的错误,但原因相同:UserManager对象为null,而它不应该为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 
对象引用未设置为对象的实例。
描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。
异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。
源错误:
第324行:公共异步任务密码签名(字符串用户名、字符串密码、bool ispersist、bool shouldllockout)
第325行:{
第326行:var user=await UserManager.FindByNameAsync(用户名);
第327行:if(user==null)
第328行:{
源文件:c:\Users\[UserName]\Documents\Visual Studio 2013\Projects\[My solution]\Models\IdentityConfig.cs行:326
问题:
  • 有人知道这是什么原因吗

  • 例如,对于system.web.mvc dll的版本5.2.2.0,是否可能需要更新Microsoft Aspnet标识示例代码


  • 注意:恐怕我无法确定或回忆起在错误发生之前我所做的更改。我已经有一段时间没有参与这个项目了。

    经过很多努力,我找到了答案:

    出于某种原因,启动文件(~/App\u 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: "");
            }
        }
    
    公共部分类启动
    {
    //有关配置身份验证的详细信息,请访问http://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder应用程序)
    {
    //将数据库上下文、用户管理器和角色管理器配置为每个请求使用一个实例
    app.CreatePerOwinContext(ApplicationDbContext.Create);
    app.CreatePerOwinContext(ApplicationUserManager.Create);
    app.CreatePerOwinContext(ApplicationRoleManager.Create);
    app.CreatePerOwinContext(ApplicationSignInManager.Create);
    //使应用程序能够使用cookie存储登录用户的信息
    //以及使用cookie临时存储用户登录第三方登录提供商的信息
    //配置登录cookie
    app.UseCookieAuthentication(新的CookieAuthenticationOptions
    {
    AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
    LoginPath=新路径字符串(“/Account/Login”),
    Provider=新CookieAuthenticationProvider
    {
    //允许应用程序在用户登录时验证安全戳。
    //这是一种安全功能,在您更改密码或向帐户添加外部登录时使用。
    OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
    validateInterval:TimeSpan.FromMinutes(30),
    regenerateIdentity:(管理器,用户)=>user.GenerateUserIdentityAsync(管理器))
    }
    });
    app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
    //允许应用程序在验证双因素身份验证过程中的第二个因素时临时存储用户信息。
    app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie,TimeSpan.FromMinutes(5));
    //使应用程序能够记住第二个登录验证因素,如电话或电子邮件。
    //选中此选项后,登录过程中的第二步验证将在您登录的设备上被记住。
    //这类似于登录时的RememberMe选项。
    app.useTowFactoryMemberBrowserCookie(DefaultAuthenticationTypes.TwoFactoryRememberBrowserCookie);
    //取消注释以下行以启用使用第三方登录提供程序登录
    //app.UseMicrosoftAccountAuthentication(
    //客户ID:“,
    //客户机密:);
    //app.UseTwitterAuthentication(
    //消费市场:“,
    //消费者信用: