【问题标题】:UserManager always null in ASPNET Identity 2 appUserManager 在 ASPNET Identity 2 应用程序中始终为空
【发布时间】:2014-12-09 02:00:32
【问题描述】:

设置:

我有一个 MVC 5 应用程序,其中包含许多使用我自己导出的模板创建的库项目。导出的模板运行良好。

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

我没有接触过 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 对象为空。

我更新了microsoft.aspnet.identity包,但是在尝试登录或新建用户时仍然出现错误(登录页面显示ok,但是点击登录按钮时出现错误)

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

错误:

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

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 版本进行更新?

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

【问题讨论】:

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


    【解决方案1】:

    经过千辛万苦,终于找到了答案:

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

    所以我从 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 包。我没有手动触摸文件,所以仍然不确定这是怎么发生的。

    【讨论】:

    • 您好,我在哪里可以找到 ApplicationRoleManager?
    • 我遵循了一些将 OWIN/Authentication 添加到现有 MVC 项目中的教程,但是我不断收到以下“值不能为空。参数名称:userManager”在我的 Startup.cs 类中app.CreatePerOwinContext(ApplicationSignInManager.Create); app.CreatePerOwinContext(CourseContext.Create); app.CreatePerOwinContext(ApplicationUserManager.Create);比较我的代码,发现我需要做的就是将 ApplicationUserManager.Create 移到 ApplicationSignInManager.Create 之前
    • 我遇到了同样的问题,这是因为 Startup.cs 文件中缺少以下行:ConfigureAuth(app);
    猜你喜欢
    • 2019-09-09
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多