的UserManager总是ASPNET身份2应用空 [英] UserManager always null in ASPNET Identity 2 app

查看:234
本文介绍了的UserManager总是ASPNET身份2应用空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置:

我有一个MVC应用5与多个库项目,开创用我自己的出口模板。导出的模板已正常工作。

我使用ASPNET身份。我只是使用Microsoft ASPNET身份样的副本,它在相关的NuGet封装,我已经编织成我出口的模板。这一直是工作的罚款。

我没碰过的ASPNET身份2样品中提供的文件。

在IdentityConfig.cs文件发生错误。

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

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

不过...

虽然应用程序加载,每当我试图登录或创建新的用户,新的错误出现(如下图所示),从根本上指出ASPNET身份的UserManager对象为null。

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

获得关于system.web.mvc参考错误之前,我就可以登录并在我的闲暇注册用户。

错误:

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

 对象引用未设置到对象的实例。说明:执行当前Web请求的执行过程中发生未处理的异常。请查看有关错误的详细信息的堆栈跟踪以及它起源于code。异常详细信息:System.NullReferenceException:对象不设置到对象的实例。源错误:
324线:公共异步任务< SignInStatus> PasswordSignIn(用户名字符串,字符串密码,布尔isPersistent,布尔shouldLockout)
325线:{
326线:VAR用户=等待UserManager.FindByNameAsync(用户名);
327线:如果(用户== NULL)
328线:{源文件:C:\\用户\\ [用户​​名] \\文档\\的Visual Studio 2013 \\项目\\ [我的解决方法] \\ \\型号线IdentityConfig.cs:326

问:


  1. 有谁知道这可能是导致此?


  2. 可以,例如,它是可能的更新为system.web.mvc DLL的5.2.2.0版本的Microsoft ASPNET标识样本code需求?


请注意:我怕我不能确定或回顾一下,我改变了错误开始发生之前。我没有工作在这个项目上一段时间。


解决方案

我有,多的痛苦之后,找到了答案:

由于某种原因,启动文件(〜/ App_Startup / Startup.Auth.cs),这应该持有code配置owin,没有。不知道这是如何发生的。

所以我复制从Microsoft.Aspnet.Identity.Samples code中的相应文件,它现在的作品。在code是:

 公共部分类启动
    {
        //欲了解更多有关配置验证,请访问http://go.microsoft.com/fwlink/?LinkId=301864
        公共无效ConfigureAuth(IAppBuilder应用程序)
        {
            //配置分贝范围内,用户管理和角色管理器使用每个请求的单个实例
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext< ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext< ApplicationRoleManager>(ApplicationRoleManager.Create);
            app.CreatePerOwinContext< ApplicationSignInManager>(ApplicationSignInManager.Create);            //使应用程序能够使用cookie来存储信息,在用户签订
            //并利用co​​okie来临时存储有关用户记录的信息与第三方供应商登录
            //配置在cookie中的标志
            app.UseCookieAuthentication(新CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LOGINPATH =新PathString(/帐号/登录),
                供应商=新CookieAuthenticationProvider
                {
                    //允许应用验证安全戳当用户登录英寸
                    //这是一项安全功能,当您更改密码或添加外部登录到您的帐户被使用。
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity< ApplicationUserManager,ApplicationUser>(
                        validateInterval:TimeSpan.FromMinutes(30),
                        regenerateIdentity:(经理,用户)=> user.GenerateUserIdentityAsync(经理))
                }
            });
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);            //使应用程序能够临时存储时,他们正在核实在双因素身份验证过程的第二个因素用户信息。
            app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie,TimeSpan.FromMinutes(5));            //使应用程序能够记住第二次登录验证的因素,如电话或电子邮件。
            //一旦选中此选项,在登录过程中验证的第二个步骤将是你从登录的设备记住。
            //这是类似的,当你登录了rememberMe选项。
            app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);            //取消注释以下行来实现与第三方供应商登录登录
            //app.UseMicrosoftAccountAuthentication(
            //的clientId:,
            // clientSecret:);            //app.UseTwitterAuthentication(
            // consumerKey:,
            // consumerSecret:);            //app.UseFacebookAuthentication(
            // APPID:,
            // appSecret:);            //app.UseGoogleAuthentication(
            //的clientId:,
            // clientSecret:);
        }
    }

我一直在使用code previously,但没有在一个点删除owin包。我没有手动触摸文件,所以还不能确定这是如何发生的。

Setup:

I have an MVC 5 app with a number of library projects, created using my own exported templates. The exported templates have been working fine.

I am using ASPNET Identity. I am just using a copy of the Microsoft Aspnet Identity Sample as supplied in the relevant NuGet package, that I have woven into my exported templates. This has been working fine.

I have not touched the files supplied in the ASPNET Identity 2 sample.

The error happens in the IdentityConfig.cs file.

For some reason, it started coming up with an error stating that it could not load the file for System.Web.Mvc, as it could not find version 5.1.0.0.

As a result, I used NuGet to update the Microsoft.Aspnet.Mvc package. This installed version 5.2.2.0 of system.web.mvc, and this effectively cleared that error.

However...

Although the application loads, whenever I try to login or create a new user, a new error comes up (shown below), essentially stating that the ASPNET Identity UserManager object was null.

I updated the microsoft.aspnet.identity package, but the error still happens when trying to login or create a new user (the login page displays ok, but the error happens when you click the log in button)

Before getting the error regarding the system.web.mvc reference, I could log in and register users at my leisure.

Error:

This is the error shown when I try to login. When I try to register a new user, I get a different error, but with the same cause: the UserManager object is null, when it shouldn't be.

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 

Question:

  1. Does anyone know what could be causing this?

  2. Could, for example, it be possible that the Microsoft Aspnet Identity sample code needs updating for version 5.2.2.0 of the system.web.mvc dll?

NOTE: I'm afraid I can't determine or recall what I changed just before the errors started happening. I haven't been working on this project for a while.

解决方案

I have, after much pain, found the answer:

For some reason, the Startup file (~/App_Startup/Startup.Auth.cs), that was supposed to hold code to configure owin, did not. Not sure how this happened.

So I copied the corresponding file from the Microsoft.Aspnet.Identity.Samples code and it now works. The code is:

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: "");
        }
    }

I had been using the code previously, but did remove the owin packages at one point. I did not touch the file manually, so still not sure how this happened.

这篇关于的UserManager总是ASPNET身份2应用空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆