2015-06-20 59 views
7

我想实现自己的DAL为asp.net标识2.0与我需要的功能。我不需要帐户锁定功能。但是,当我尝试调用商店不实施IUserLockoutStore <TUser>

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

我得到System.NotSupportedException:Store does not implement IUserLockoutStore<TUser>.

那么,为什么我需要实现IUserLockoutStore如果我不需要它?

回答

10

看到这个答案:When implementing your own IUserStore, are the "optional" interfaces on the class actually optional?

您需要欺骗或覆盖你想在您的商店打电话来实现一个不使用“可选”锁定商店的方法。

您可能会不愉快地惊讶地发现,您还需要为双因素实现“可选”接口。除非你有双重因素,否则请使用下面的相同答案。

首先,虽然,这里的默认实现:

public virtual async Task<SignInStatus> 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<int> GetAccessFailedCountAsync(MyUser user) 
    { 
     throw new NotImplementedException(); 
    } 

    public Task<bool> GetLockoutEnabledAsync(MyUser user) 
    { 
     return Task.Factory.StartNew<bool>(() => false); 
    } 

    public Task<DateTimeOffset> GetLockoutEndDateAsync(MyUser user) 
    { 
     throw new NotImplementedException(); 
    } 

    public Task<int> 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<SignInStatus> 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/http://eliot-jones.com/Code/asp-identity/MyUserStore.cs

+0

末链接不工作了。你能更新吗? – Vinigas