Asp.net-Mvc-5

商店沒有實現 IUserLockoutStore<TUser>

  • August 30, 2018

我正在嘗試使用我需要的功能為 asp.net Identity 2.0 實現自己的 DAL。我不需要帳戶鎖定功能。但是當我嘗試打電話時

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

我明白了System.NotSupportedException:Store does not implement IUserLockoutStore&lt;TUser&gt;.

那麼如果我不需要它,為什麼我需要實現 IUserLockoutStore 呢?

看到這個答案:當實現你自己的 IUserStore 時,類上的“可選”介面實際上是可選的嗎?

您將需要欺騙或覆蓋您嘗試在商店中呼叫的方法,以實現不使用“可選”鎖定商店的方法。

您可能會驚訝地發現您還需要為兩個因素實現“可選”介面。除非您確實有兩個因素的方法,否則請使用下面的相同答案。

不過,首先,這是預設實現:

public virtual async Task&lt;SignInStatus&gt; PasswordSignInAsync(string userName, string password, bool isPersistent, bool shouldLockout)
       {
          ...
           if (await UserManager.IsLockedOutAsync(user.Id).WithCurrentCulture())
           {
               return SignInStatus.LockedOut;
           }
           if (await UserManager.CheckPasswordAsync(user, password).WithCurrentCulture())
           {
               return await SignInOrTwoFactor(user, isPersistent).WithCurrentCulture();
           }
           ...
           return SignInStatus.Failure;
       }

一個答案:創建無用的商店。

   #region LockoutStore
   public Task&lt;int&gt; GetAccessFailedCountAsync(MyUser user)
   {
       throw new NotImplementedException();
   }

   public Task&lt;bool&gt; GetLockoutEnabledAsync(MyUser user)
   {
       return Task.Factory.StartNew&lt;bool&gt;(() =&gt; false);
   }

   public Task&lt;DateTimeOffset&gt; GetLockoutEndDateAsync(MyUser user)
   {
       throw new NotImplementedException();
   }

   public Task&lt;int&gt; IncrementAccessFailedCountAsync(MyUser user)
   {
       throw new NotImplementedException();
   }

   public Task ResetAccessFailedCountAsync(MyUser user)
   {
       throw new NotImplementedException();
   }

   public Task SetLockoutEnabledAsync(MyUser user, bool enabled)
   {
       throw new NotImplementedException();
   }

   public Task SetLockoutEndDateAsync(MyUser user, DateTimeOffset lockoutEnd)
   {
       throw new NotImplementedException();
   }
   #endregion
}

另一個解決方案:覆蓋只是不使用它。

public virtual async Task&lt;SignInStatus&gt; PasswordSignInAsync(string userName, string password, bool isPersistent, bool shouldLockout)
       {
          ...
           if (false)
           {
               return SignInStatus.LockedOut;
           }
           if (await UserManager.CheckPasswordAsync(user, password).WithCurrentCulture())
           {
               return await SignInOrTwoFactor(user, isPersistent).WithCurrentCulture();
           }
           ...
           return SignInStatus.Failure;
       }

cf/ https://github.com/aspnet/AspNetIdentity/blob/master/src/Microsoft.AspNet.Identity.Owin/SignInManager.cs

引用自:https://stackoverflow.com/questions/30957060