c# - SignInManager.PasswordSignInAsync 抛出空对象异常

标签 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,它是 AccountController 上类型为 ApplicationSignInManager 的一个对象。从上面的行调用的 SignInManagerget 访问器是

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

我看过the source 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

对于可能导致此 NullReferenceException 的原因,我将不胜感激。如果我可以添加更多详细信息(或删除一些(大笑!)),请告诉我。这是我和我公司的第一个 MVC 项目,所以我确定我破坏了一些东西。

编辑

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

最佳答案

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

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

方法public void ConfigureAuth(IAppBuilder app)App_Start\Startup.Auth.cs 包括这个之后一切都应该工作。

关于c# - SignInManager.PasswordSignInAsync 抛出空对象异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30220597/

相关文章:

c# - 软删除数据但在获取时出现问题 | EF 核心,C#

时间:2019-03-17 标签:c#bytearraysize

asp.net - 如何在 Asp.NET MVC 中使用 RedirectToAction() 传递多个对象?

c# - "Upload"使用jQuery上传文件不点击

jquery - Javascript 字符串化对象

c# - 使用属性和约定路由

javascript - 无法使用 Javascript 使 RegEx 模式在 MVC 5 View 中正常工作

c# - 如何将一个c#文件包含到另一个c#文件中?

c# - 这两个委托(delegate)声明有什么区别?

asp.net-mvc - ReCAPTCHA v2 无法在 IE 兼容性 View 中工作