Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

Implement User Lock manually

$
0
0

Hi,

I am using Asp.Net identity with WebAPI and n-tier Architecture, where the WebAPI has no access to Asp.Net Identity features directly but through a service layer.

In my business logic, i have to send mail to admin to unlock user through a password reset since the first account lock.

Here my method which perform the logic. no mail is sent. please help me to have a solution.

/// <summary>
        /// Authenticates the user.
        /// </summary>
        /// <param name="request">The auth user profile request.</param>
        /// <returns>The auth user profile response</returns>
        public AuthUserProfileResponsePivot AuthenticateUser(AuthUserProfileRequestPivot request)
        {
            AuthUserProfileResponsePivot response = new AuthUserProfileResponsePivot();

            AuthUserProfile user = _unitOfWork.UserProfileManager.FindByName(request.AuthUserProfilePivot.UserName);

            if (user == null)
            {
                return null;
            }

            if (_unitOfWork.UserProfileManager.SupportsUserLockout &&
                _unitOfWork.UserProfileManager.IsLockedOut(user.Id))
            {
                response.IsUserLocked = true;

                return response;
            }
            if (_unitOfWork.UserProfileManager.CheckPassword(user, user.Id))
            {
                if (_unitOfWork.UserProfileManager.SupportsUserLockout && _unitOfWork.UserProfileManager.GetAccessFailedCount(user.Id) > 0)
                {
                    _unitOfWork.UserProfileManager.ResetAccessFailedCount(user.Id);
                }

                response.IsUserLocked = false;

                // Authenticate user
            }
            else
            {
                if (_unitOfWork.UserProfileManager.SupportsUserLockout)
                {
                    if (!_unitOfWork.UserProfileManager.GetLockoutEnabled(user.Id))
                    {
                        _unitOfWork.UserProfileManager.SetLockoutEnabled(user.Id, true);
                    }

                    _unitOfWork.UserProfileManager.AccessFailed(user.Id);

                    if (_unitOfWork.UserProfileManager.IsLockedOut(user.Id))
                    {
                        response.IsUserLocked = true;
                    }

                    if (user.AccessFailedCount ==
                        ConfigurationManagerHelper.GetAppSettingsToInt(Constant.UserMaxFailureLoginAttempts, 6) - 1)
                    {
                        response.ShouldSendResetMail = true;
                    }

                    return response;
                }
            }

            return new AuthUserProfileResponsePivot();
        }
/// <summary>
        /// Resets the access failed count.
        /// </summary>
        /// <param name="request">The auth user profile request.</param>
        /// <returns>The auth user profile response</returns>
        public AuthUserProfileResponsePivot ResetAccessFailedCount(AuthUserProfileRequestPivot request)
        {
            AuthUserProfile user = new AuthUserProfile();
            switch (request.FindAuthUserProfilePivotEnum)
            {
                case FindAuthUserProfilePivotEnum.FindById:
                    user = _unitOfWork.UserProfileManager.FindById(request.AuthUserProfilePivot.UserId);
                    break;
                case FindAuthUserProfilePivotEnum.FindByUserName:
                    user = _unitOfWork.UserProfileManager.FindByName(request.AuthUserProfilePivot.UserName);
                    break;
            }
            IdentityResult result = _unitOfWork.UserProfileManager.SetLockoutEnabled(user.Id, false);
            if (result.Succeeded)
            {
                _unitOfWork.UserProfileManager.ResetAccessFailedCount(user.Id);
            }

            return new AuthUserProfileResponsePivot()
            {
                ResetAccessFailedCountSucceeded = true,
                AuthUserProfilePivot = user.ToPivot()
            };
        }

Regards


Viewing all articles
Browse latest Browse all 4737


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>