Store does not implement IUserLockoutStore

前端 未结 1 1543
悲哀的现实
悲哀的现实 2021-01-11 13:25

I\'m trying to implement own DAL for asp.net Identity 2.0 with functionality that I need. I don\'t need Account Lockout functionality. But When I try to call



        
1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-11 13:54

    See this answer: When implementing your own IUserStore, are the "optional" interfaces on the class actually optional?

    You will need to trick or override the method you are trying to call in your store to implement one that does not use the "optional" lockout store.

    You may be unpleasantly surprised to find that you also need to implement the "optional" interface for two-factor. Use the same answer below to do so unless you do have a means of two-factor.

    First, though, here's the default implementation:

    public virtual async Task 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;
            }
    

    One answer: create useless stores.

        #region LockoutStore
        public Task GetAccessFailedCountAsync(MyUser user)
        {
            throw new NotImplementedException();
        }
    
        public Task GetLockoutEnabledAsync(MyUser user)
        {
            return Task.Factory.StartNew(() => false);
        }
    
        public Task GetLockoutEndDateAsync(MyUser user)
        {
            throw new NotImplementedException();
        }
    
        public Task 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
    }
    

    Another solution: override to just not use it.

    public virtual async Task 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

    0 讨论(0)
提交回复
热议问题