asp.net-mvc-5 – ASP.net身份禁用用户

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-5 – ASP.net身份禁用用户前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在MVC 5中使用新的ASP.net身份,我们如何禁止用户登录?我不想删除它们,也许只是在一段时间内禁用他们的帐户。

有没有人有任何想法,因为我没有看到一个状态列或ASP.NETUsers表上的任何东西。

解决方法

更新:正如CountZero指出的,如果您使用的是v2.1,那么您应该尝试使用先前添加的锁定功能,然后再尝试下面的解决方案。查看他们的博客文章一个完整的例子: http://blogs.msdn.com/b/webdev/archive/2014/08/05/announcing-rtm-of-asp-net-identity-2-1-0.aspx

版本2.0具有可用于锁定用户的IUserLockoutStore接口,但不利之处在于,没有OOB功能可以超越UserManager类公开的直通方法来实际利用它。例如,如果实际上将锁定计数作为标准用户名/密码验证过程的一部分实际增加将是很好的。但是,实现自己是相当微不足道的。

步骤1:创建实现IUserLockoutStore的自定义用户存储。

// I'm specifying the TKey generic param here since we use int's for our DB keys
// you may need to customize this for your environment
public class MyUserStore : IUserLockoutStore<MyUser,int>
{
    // IUserStore implementation here

    public Task<DateTimeOffset> GetLockoutEndDateAsync(MyUser user)
    {
        //..
    }

    public Task SetLockoutEndDateAsync(MyUser user,DateTimeOffset lockoutEnd)
    {
        //..
    }

    public Task<int> IncrementAccessFailedCountAsync(MyUser user)
    {
        //..
    }

    public Task ResetAccessFailedCountAsync(MyUser user)
    {
        //..
    }

    public Task<int> GetAccessFailedCountAsync(MyUser user)
    {
        //..
    }

    public Task<bool> GetLockoutEnabledAsync(MyUser user)
    {
        //..
    }

    public Task SetLockoutEnabledAsync(MyUser user,bool enabled)
    {
        //..
    }
}

步骤2:代替UserManager,在登录/注销操作中使用以下类,传递自定义用户存储的实例。

public class LockingUserManager<TUser,TKey> : UserManager<TUser,TKey>
    where TUser : class,IUser<TKey> 
    where TKey : IEquatable<TKey> 
{
    private readonly IUserLockoutStore<TUser,TKey> _userLockoutStore;

    public LockingUserManager(IUserLockoutStore<TUser,TKey> store)
        : base(store)
    {
        if (store == null) throw new ArgumentNullException("store");

        _userLockoutStore = store;
    }

    public override async Task<TUser> FindAsync(string userName,string password)
    {
        var user = await FindByNameAsync(userName);

        if (user == null) return null;

        var isUserLockedOut = await GetLockoutEnabled(user);

        if (isUserLockedOut) return user;

        var isPasswordValid = await CheckPasswordAsync(user,password);

        if (isPasswordValid)
        {
            await _userLockoutStore.ResetAccessFailedCountAsync(user);
        }
        else
        {
            await IncrementAccessFailedCount(user);

            user = null;
        }

        return user;
    }

    private async Task<bool> GetLockoutEnabled(TUser user)
    {
        var isLockoutEnabled = await _userLockoutStore.GetLockoutEnabledAsync(user);

        if (isLockoutEnabled == false) return false;

        var shouldRemoveLockout = DateTime.Now >= await _userLockoutStore.GetLockoutEndDateAsync(user);

        if (shouldRemoveLockout)
        {
            await _userLockoutStore.ResetAccessFailedCountAsync(user);

            await _userLockoutStore.SetLockoutEnabledAsync(user,false);

            return false;
        }

        return true;
    }

    private async Task IncrementAccessFailedCount(TUser user)
    {
        var accessFailedCount = await _userLockoutStore.IncrementAccessFailedCountAsync(user);

        var shouldLockoutUser = accessFailedCount > MaxFailedAccessAttemptsBeforeLockout;

        if (shouldLockoutUser)
        {
            await _userLockoutStore.SetLockoutEnabledAsync(user,true);

            var lockoutEndDate = new DateTimeOffset(DateTime.Now + DefaultAccountLockoutTimeSpan);

            await _userLockoutStore.SetLockoutEndDateAsync(user,lockoutEndDate);
        }
    }
}

例:

[AllowAnonymous]
    [HttpPost]
    public async Task<ActionResult> Login(string userName,string password)
    {
        var userManager = new LockingUserManager<MyUser,int>(new MyUserStore())
        {
            DefaultAccountLockoutTimeSpan = /* get from appSettings */,MaxFailedAccessAttemptsBeforeLockout = /* get from appSettings */
        };

        var user = await userManager.FindAsync(userName,password);

        if (user == null)
        {
            // bad username or password; take appropriate action
        }

        if (await _userManager.GetLockoutEnabledAsync(user.Id))
        {
            // user is locked out; take appropriate action
        }

        // username and password are good
        // mark user as authenticated and redirect to post-login landing page
    }

如果你想手动锁定一个人,你可以设置你在MyUserStore.GetLockoutEnabledAsync()中检查的任何标志。

猜你在找的asp.Net相关文章