asp.net-mvc-5 - 存储未实现 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;
        }

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

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

相关文章:

c# - Mvc 在回发后保留值

ajax - ASP.NET MVC 5 错误

asp.net - 两个不同的 OWIN 应用程序能否授权一个 OAuth2 不记名访问 token ?

asp.net-mvc - AspNet Identity 如何与我的模型

asp.net-core - 如何将 IdentityManager 与 AspNetCoreIdentity 结合使用

azure - ASP.NET Core 2.2 - 密码重置在 Azure 上不起作用( token 无效)

asp.net-mvc - 模板和 htmlAttributes 的自定义编辑器

url - Blob 当前是否有 SAS URL?

asp.net-mvc - ASP.Net MVC 5 + SignalR + Ninject

c# - 在没有 asp 的情况下,在创建新 cookie 之前检查声明(电子邮件)。使用 asp net 核心社交登录的核心身份存储