我已为我的应用程序创建了自定义IUserStore<TUser,int>
。我已经实现了我需要的接口,
IUserStore<TUser, int>,
IUserRoleStore<TUser, int>,
IUserLockoutStore<TUser, int>,
IUserPasswordStore<TUser, int>
但是当我打电话时
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);
我得到一个例外
Store does not implement IUserTwoFactorStore<TUser>.
我在我的应用程序中的任何地方都没有使用双因素身份验证。为什么它希望我实现该接口?我是否需要实现所有这些接口,即使我实际上没有使用它们?
答案 0 :(得分:16)
实际上IUserTwoFactorStore
界面非常简单,到目前为止我的实现(我也不使用两个因子身份验证)是这样的:
....
public Task<bool> GetTwoFactorEnabledAsync(User user)
{
return Task.FromResult(false);
}
public Task SetTwoFactorEnabledAsync(User user, bool enabled)
{
throw new NotImplementedException();
}
虽然我几分钟前刚刚做过,并没有彻底测试整个应用程序,但它确实有效。
答案 1 :(得分:14)
我遇到了同样的问题。目前,当SignInManager.SignInOrTwoFactor方法盲目地检查GetTwoFactorAuthentication时,它会在UserStore未实现IUserTwoFactorStore时抛出异常。
我相信Microsoft打算在自定义SignInManager类中重写SignInManager PasswordSignInAsync方法。不幸的是,我找不到任何文件或样本。
这是我为解决这个问题而实现的SignInManager包装器类:
public class EnhancedSignInManager<TUser, TKey> : SignInManager<TUser, TKey>
where TUser : class, IUser<TKey>
where TKey : IEquatable<TKey>
{
public EnhancedSignInManager(
UserManager<TUser, TKey> userManager,
IAuthenticationManager authenticationManager)
: base(userManager, authenticationManager)
{
}
public override async Task SignInAsync(
TUser user,
bool isPersistent,
bool rememberBrowser)
{
var userIdentity = await CreateUserIdentityAsync(user).WithCurrentCulture();
// Clear any partial cookies from external or two factor partial sign ins
AuthenticationManager.SignOut(
DefaultAuthenticationTypes.ExternalCookie,
DefaultAuthenticationTypes.TwoFactorCookie);
if (rememberBrowser)
{
var rememberBrowserIdentity = AuthenticationManager
.CreateTwoFactorRememberBrowserIdentity(ConvertIdToString(user.Id));
AuthenticationManager.SignIn(
new AuthenticationProperties { IsPersistent = isPersistent },
userIdentity,
rememberBrowserIdentity);
}
else
{
AuthenticationManager.SignIn(
new AuthenticationProperties { IsPersistent = isPersistent },
userIdentity);
}
}
private async Task<SignInStatus> SignInOrTwoFactor(TUser user, bool isPersistent)
{
var id = Convert.ToString(user.Id);
if (UserManager.SupportsUserTwoFactor
&& await UserManager.GetTwoFactorEnabledAsync(user.Id)
.WithCurrentCulture()
&& (await UserManager.GetValidTwoFactorProvidersAsync(user.Id)
.WithCurrentCulture()).Count > 0
&& !await AuthenticationManager.TwoFactorBrowserRememberedAsync(id)
.WithCurrentCulture())
{
var identity = new ClaimsIdentity(
DefaultAuthenticationTypes.TwoFactorCookie);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, id));
AuthenticationManager.SignIn(identity);
return SignInStatus.RequiresVerification;
}
await SignInAsync(user, isPersistent, false).WithCurrentCulture();
return SignInStatus.Success;
}
public override async Task<SignInStatus> PasswordSignInAsync(
string userName,
string password,
bool isPersistent,
bool shouldLockout)
{
if (UserManager == null)
{
return SignInStatus.Failure;
}
var user = await UserManager.FindByNameAsync(userName).WithCurrentCulture();
if (user == null)
{
return SignInStatus.Failure;
}
if (UserManager.SupportsUserLockout
&& await UserManager.IsLockedOutAsync(user.Id).WithCurrentCulture())
{
return SignInStatus.LockedOut;
}
if (UserManager.SupportsUserPassword
&& await UserManager.CheckPasswordAsync(user, password)
.WithCurrentCulture())
{
return await SignInOrTwoFactor(user, isPersistent).WithCurrentCulture();
}
if (shouldLockout && UserManager.SupportsUserLockout)
{
// If lockout is requested, increment access failed count
// which might lock out the user
await UserManager.AccessFailedAsync(user.Id).WithCurrentCulture();
if (await UserManager.IsLockedOutAsync(user.Id).WithCurrentCulture())
{
return SignInStatus.LockedOut;
}
}
return SignInStatus.Failure;
}
}
我希望它有所帮助。干杯
答案 2 :(得分:4)
我遇到了同样的问题而且我不想实施IUserTwoFactorStore<TUser, TKey>
只是为了说我没有实现它。但是如果我最终想要实现它(我预期我会这样做),我也不想回去捣乱。所以我认为未来证明(和可重复使用)的解决方案将是:(受@ gjsduarte&#39的回答启发)
public class SafeUserManager<TUser, TKey> : UserManager<TUser, TKey>
{
public override Task<bool> GetTwoFactorEnabledAsync(TKey userId)
{
return Store is IUserTwoFactorStore<TUser, TKey>
? base.GetTwoFactorEnabledAsync(userId)
: Task.FromResult(false);
}
}
对其他Get[feature]EnabledAsync(TKey userId)
方法做同样的事情可能是个好主意。