SignInManager.PasswordSignInAsync抛出空对象异常

时间:2015-05-13 16:35:35

标签: c# asp.net-mvc asp.net-mvc-5 nullreferenceexception owin

AccountController.cs包含AccountController课程。该类的Login方法失败。具体地,

var result = await SignInManager.PasswordSignInAsync(
    model.Email, model.Password, model.RememberMe, shouldLockout: true);
Login方法中的

正在抛出System.NullReferenceException

我们已经确认model.Emailmodel.Passwordmodel.RememberMe不为空。下一步是深入研究SignInManager,这是AccountControllerApplicationSignInManager上的对象。 get的{​​{1}}访问者(从上面一行调用)是

SignInManager

我改写为

public ApplicationSignInManager SignInManager
{
    get
    {
        return _signInManager ??
            HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
    }
    // private set...
}

调试并确保public ApplicationSignInManager SignInManager { get { var c = HttpContext.GetOwinContext(); var m = c.Get<ApplicationSignInManager>(); return _signInManager ?? m; } // private set... } 访问者未返回get。它没有返回null。这告诉我null中需要的内容是PasswordSignInAsync

我查看了null的{​​{3}},它在防止空引用错误方面做得非常好。我还深入研究了PasswordSignInAsync所调用的方法。我根本不知道什么可能引发空引用错误。

错误的堆栈跟踪非常神秘,我不会在其中看到任何有用的信息。但为了完整起见,这里是:

PasswordSignInAsync

我感谢任何可能导致此问题的帮助或指导[NullReferenceException: Object reference not set to an instance of an object.] Microsoft.AspNet.Identity.<CreateAsync>d__0.MoveNext() +2112 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24 Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +123 Microsoft.AspNet.Identity.Owin.<SignInAsync>d__2.MoveNext() +408 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21 Microsoft.AspNet.Identity.CultureAwaiter.GetResult() +63 Microsoft.AspNet.Identity.Owin.<SignInOrTwoFactor>d__23.MoveNext() +2055 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24 Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +68 Microsoft.AspNet.Identity.Owin.<PasswordSignInAsync>d__29.MoveNext() +1828 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24 ApexRemingtonExternal.Controllers.<Login>d__2.MoveNext() in c:\Users\me\sc\project\Controllers\AccountController.cs:91 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21 System.Threading.Tasks.TaskHelpersExtensions.ThrowIfFaulted(Task task) +61 System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +114 System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +66 System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49 System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +117 System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +323 System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +44 System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50 System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +72 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185 System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40 System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44 System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39 System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651688 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 。如果我可以添加更多细节(或删除一些(哈哈!)),请告诉我。这是我和我公司的第一个MVC项目,所以我确定我已经破坏了一些东西。

修改

我应该补充一点,我从项目中剥离了实体框架,转而使用LINQ to SQL,这是我们在所有其他项目中所依赖的。这意味着我必须实现自己的IUserStore版本,IRoleStore等。

4 个答案:

答案 0 :(得分:11)

有时(特别是如果您更新Microsoft.AspNet.Identity.Owin),您可能忘记添加行

app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

App_Start \ Startup.Auth.cs 中的方法public void ConfigureAuth(IAppBuilder app) 包括这一切后,一切都应该有效。

答案 1 :(得分:7)

通常我的习惯,问题与症状无关。当然SignInManager.PasswordSignInAsync没有被打破。这是我的代码被打破了。这使得这个问题没有实际意义,这让我想要删除这个问题。

然而,这里有一个值得学习的教训。就我而言,它是as关键字。放弃了EF,如编辑中所提到的,我不得不提供几个接口的实现。我必须提供的一种方法是GetRolesAsync,它返回Task<IList<String>>(在我的情况下)。我从未处理IList。事实证明as是我所需要的。但我试图将IQueryable投射到IList,但这并不起作用。结果:as只返回null。谢谢,as。这很有用。

呀。我可能会删除这个问题。

答案 2 :(得分:0)

游戏后期,但是我今天遇到了这个错误,发现空引用异常的原因是ApplicationUser中的null属性。

当您在Applicationuser中创建自定义声明时,就会发生这种情况。 当您调用SignInManager.PasswordSignInAsync时,它将生成GenerateUserIdentity,其中将包括您的自定义声明。那里的任何空对象都将引发NULL REFERENCE EXCEPTION。

显示一些示例代码(请参阅注释):

public class ApplicationUser : IdentityUser
{
    ...//some additional properties goes here

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

        //my custom claims
        List<Claim> claims = new List<Claim>
        {
            new Claim("FirstName", FirstName.ToString()),
            new Claim("MiddleName", MiddleName.ToString()),
            new Claim("LastName", LastName.ToString()),
            new Claim("Position", Position.ToString()) //if Position is NULL, it will throw null reference exception
        };

        userIdentity.AddClaims(claims);

        return userIdentity;
    }
}

我希望这会在将来对某人有所帮助。

答案 3 :(得分:0)

就我而言,就是数据!

我已经在我的 test 数据库中手动将TwoFactorEnabled的值设置为true,对于该特定用户PhoneNumber为空,并且PhoneNumberConfirmed设置为假。 在PhoneNumber中插入一个值并将PhoneNumberConfirmed标记为true可以使异常消失。