2015-06-20 12 views
7

Sto cercando di implementare il proprio DAL per asp.net Identity 2.0 con le funzionalità di cui ho bisogno. Non ho bisogno della funzionalità di blocco dell'account. Ma quando provo a chiamareStore non implementa IUserLockoutStore <TUser>

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

ottengo System.NotSupportedException:Store does not implement IUserLockoutStore<TUser>.

Quindi perché dovrei necessario implementare IUserLockoutStore se non ho bisogno di esso?

risposta

10

Vai a questa risposta: When implementing your own IUserStore, are the "optional" interfaces on the class actually optional?

Avrete bisogno di ingannare o sovrascrivere il metodo che si sta tentando di chiamare nel tuo negozio per implementare uno che non utilizza il negozio di blocco "optional".

Potrebbe essere spiacevolmente sorpreso scoprire che è anche necessario implementare l'interfaccia "opzionale" per due fattori. Usa la stessa risposta qui sotto per farlo, a meno che tu non abbia un mezzo a due fattori.

Prima, però, ecco l'implementazione di default:

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; 
     } 

Una risposta: creare negozi inutili.

#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 
} 

Un'altra soluzione: ignorare semplicemente non utilizzarlo.

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

link alla fine non funziona più. Puoi aggiornare? – Vinigas

Problemi correlati