asp.net-mvc-5 - Store 没有实现 IUserLockoutStore<TUser>

标签 asp.net-mvc-5 asp.net-identity

我正在尝试使用我需要的功能为 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<TUser>.

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

最佳答案

查看此答案:When implementing your own IUserStore, are the "optional" interfaces on the class actually optional?

您将需要欺骗或重写您尝试在商店中调用的方法,以实现不使用“可选”锁定商店的方法。

您可能会惊讶地发现您还需要为双因素实现“可选”接口(interface)。使用下面相同的答案来这样做,除非你确实有双因素的方法。

不过,首先,这里是默认实现:

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

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

关于asp.net-mvc-5 - Store 没有实现 IUserLockoutStore<TUser>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33659768/

相关文章:

c# - User.Identity.GetUserId() 登录成功后返回null

c# - 如何为 ASP.NET MVC 5 创建依赖注入(inject)?

asp.net-mvc - 试图从身份中删除角色

c# - 如何在 ASP.Net MVC 5 View 中获取 ApplicationUser 的自定义属性值?

javascript - 如何将这两个表与两个模型合并?

c# - 没有 Owin 的 Asp.Net MVC 5?

c# - 导航属性为空

c# - asp.net 身份表未创建

c# - 持久性 Cookie 在浏览器关闭时被删除 - Identity 2.0

asp.net-mvc - 阻止未确认电子邮件的用户登录 ASP.NET MVC Web API Identity(OWIN 安全)