簡體   English   中英

無法使用 ASP.NET Identity 登錄,值不能為空。參數名稱:manager

[英]Can not sign in using ASP.NET Identity, Value cannot be null.Parameter name: manager

當我到達這一點時,我有這個登錄方法它給了我一個錯誤

var result = SignInManager.PasswordSignIn(userName, password, false, false);

它顯示的例外是這個

Microsoft.AspNet.Identity.Owin.dll 中出現“System.ArgumentNullException”類型的異常,但未在用戶代碼中處理

附加信息:值不能為空。

但這是它顯示的主要錯誤

{"值不能為空。\\r\\n參數名稱:manager"}

這是堆棧跟蹤

   at Microsoft.AspNet.Identity.Owin.SignInManagerExtensions.PasswordSignIn[TUser,TKey](SignInManager`2 manager, String userName, String password, Boolean isPersistent, Boolean shouldLockout)
   at MIS.Web.Controllers.PublicController.ValidateUser(String userName, String password) in C:\Users\Jack\Dropbox\Angular 2 Test Environment\MIS BackUp\MIS.Web\MIS.Web\Controllers\PublicController.cs:line 138
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()

這是我完整的登錄方法

 public string ValidateUser(string userName, string password)
        {
            var userStore = new UserStore<IdentityUser>();
            userStore.Context.Database.Connection.ConnectionString =
                System.Configuration.ConfigurationManager
                .ConnectionStrings["Test"].ConnectionString;
            var manager = new UserManager<IdentityUser>(userStore);


            var user = manager.Find(userName, password);
            manager.UpdateSecurityStampAsync(user.Id);



            var result = SignInManager.PasswordSignIn(userName, password, false, false);
            if (result == SignInStatus.Success)
                return userName;

            ModelState.AddModelError("", "Invalid login attempt.");
            return null;
        }

問題是我在將我的OWIN擴展到基於role和基於groupAuthorization之前創建的用戶變得無效,所以我每次創建新用戶時都會添加這個新行,它的SecurtyStamp需要像這樣更新

await UserManager.UpdateSecurityStampAsync(user.Id);

希望這可以幫助任何人

請確保 Id 和 UserName 不為 null 或為空!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM