2015-05-13 16 views
12

AccountController.cs enthält die AccountController-Klasse. Die Methode Login dieser Klasse schlägt fehl. Speziell wirdSignInManager.PasswordSignInAsync Null-Objekt-Ausnahme auslösen

var result = await SignInManager.PasswordSignInAsync(
    model.Email, model.Password, model.RememberMe, shouldLockout: true); 

innerhalb des Login Verfahren werfen System.NullReferenceException. Ich habe überprüft, dass model.Email, model.Password und model.RememberMe nicht null sind Der nächste Schritt ist in zu SignInManager zu graben, die auf AccountController vom Typ ApplicationSignInManager ein Objekt ist. Die get Accessor für SignInManager, die von der Linie oben genannt wird, war

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

, die ich als

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

zu debuggen und stellen Sie sicher, dass die get Accessor nicht null zurückkehrte. Es kommt nicht zurück null. Dies sagt mir, dass etwas innerhalb von PasswordSignInAsyncnull erforderlich ist.

Ich habe the source von PasswordSignInAsync betrachtet und es ist ein ziemlich guter Job, gegen Null Referenzfehler zu schützen. Ich habe auch tiefer in die von PasswordSignInAsync genannten Methoden gegraben. Ich sehe einfach nicht, was einen Nullreferenzfehler werfen könnte.

Der Stack-Trace aus dem Fehler ist ziemlich kryptisch, und ich habe nichts darin sehen, das hilft. Aber der Vollständigkeit halber, hier ist sie:

[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 

ich jede Hilfe dankbar würde oder Anleitung, was könnte diese NullReferenceException verursachen. Bitte lassen Sie mich wissen, wenn ich mehr Details hinzufügen kann (oder entfernen Sie einige (lol!)). Dies ist das erste MVC-Projekt meines und meines Unternehmens, also bin ich mir sicher, dass ich etwas kaputt gemacht habe.

bearbeiten

Ich sollte hinzufügen, dass ich aus dem Projekt Entity Framework für LINQ to SQL, das, was wir in allen unseren anderen Projekten stützen sich auf ist abgezogen. Das bedeutet, dass ich meine eigenen Versionen von IUserStore, IRoleStore usw. implementieren musste.

Antwort

7

Wie in der Regel meine Gewohnheit, hatte das Problem nichts mit dem Symptom zu tun. Natürlich ist SignInManager.PasswordSignInAsync nicht kaputt. Es war mein Code, der kaputt war. Das macht diese Frage strittig, was mich bittet, die Frage zu löschen.

Es wird jedoch eine Lektion gelernt, hier zu sein. In meinem Fall war es das as Schlüsselwort. Nachdem ich EF verlassen hatte, wie in der Bearbeitung erwähnt, musste ich Implementierungen mehrerer Schnittstellen bereitstellen. Eine Methode, die ich liefern musste, ist GetRolesAsync, die Task<IList<String>> (in meinem Fall) zurückgibt. Ich habe noch nie mit IList gehandelt. Stellt sich heraus as ist was ich brauchte. Aber ich habe versucht, eine IQueryable zu einem IList zu werfen, die nicht funktioniert. Das Ergebnis: as gibt einfach null zurück. Danke, as. Das ist nützlich.

Ja. Ich werde diese Frage wahrscheinlich löschen.

+2

Nicht löschen! Ich habe gerade den gleichen Fehler, habe das gefunden, und es hat mir geholfen festzustellen, "das Problem hatte nichts mit dem Symptom zu tun", was mir eine Menge Zeit erspart hätte. Mine war eine Null-Referenz-Ausnahme, die durch die aufgetretene Ausnahme verborgen war. –

+0

Hey Jeff, fwiw, das ist der Unterschied zwischen 'as' und einer direkten Besetzung (siehe [this] (http://stackoverflow.com/a/132467/1252541)). – shortstuffsushi

9

Manchmal (besonders wenn Sie Microsoft.AspNet.Identity.Owin aktualisieren) können Sie vergessen Linie

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

zu Verfahren public void ConfigureAuth(IAppBuilder app) in App_Start \ Startup.Auth.cs Nach Einbeziehung dieser alles sollte Werke hinzuzufügen.