Abp Core添加手机登录功能

交流QQ号:555913397
有什么问题可以加群大家一起交流

上一章讲解了如何添加短信验证码登录,这一章给大家讲讲如何用"手机号"加"密码"登录,多的不说了,上代码.

在Core/Authorization/LoginManager.cs

        /// <summary>
        /// 手机号登录
        /// </summary>
        /// <param name="phoneNumber">手机号</param>
        /// <param name="plainPassword">密码</param>
        /// <param name="tenancyName">租户名</param>
        /// <param name="shouldLockout">锁定</param>
        /// <returns></returns>
        [UnitOfWork]
        public virtual async Task<AbpLoginResult<Tenant, User>> LoginByMobileAsync(string phoneNumber, string plainPassword, string tenancyName = null, bool shouldLockout = true)
        {
            var result = await LoginByMobileAsyncInternal(phoneNumber, plainPassword, tenancyName, shouldLockout);
            await SaveLoginAttempt(result, tenancyName, phoneNumber);
            return result;
        }
        /// <summary>
        /// 手机号登录内部方法
        /// </summary>
        /// <param name="phoneNumber">手机号码</param>
        /// <param name="plainPassword">密码</param>
        /// <param name="tenancyName">租户名</param>
        /// <param name="shouldLockout">是否锁定</param>
        /// <returns></returns>
        protected virtual async Task<AbpLoginResult<Tenant, User>> LoginByMobileAsyncInternal(string phoneNumber, string plainPassword, string tenancyName, bool shouldLockout)
        {
            if (phoneNumber.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(phoneNumber));
            }
            if (plainPassword.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(plainPassword));
            }
            //Get and check tenant
            Tenant tenant = null;
            using (UnitOfWorkManager.Current.SetTenantId(null))
            {
                if (!MultiTenancyConfig.IsEnabled)
                {
                    tenant = await GetDefaultTenantAsync();
                }
                else if (!string.IsNullOrWhiteSpace(tenancyName))
                {
                    tenant = await TenantRepository.FirstOrDefaultAsync(t => t.TenancyName == tenancyName);
                    if (tenant == null)
                    {
                        return new AbpLoginResult<Tenant, User>(AbpLoginResultType.InvalidTenancyName);
                    }
                    if (!tenant.IsActive)
                    {
                        return new AbpLoginResult<Tenant, User>(AbpLoginResultType.TenantIsNotActive, tenant);
                    }
                }
            }
            var tenantId = tenant == null ? (int?)null : tenant.Id;
            using (UnitOfWorkManager.Current.SetTenantId(tenantId))
            {
                await UserManager.InitializeOptionsAsync(tenantId);
                var loggedInFromExternalSource = false; 
                var user = await _userStore.FindByPhoneNumberAsync(tenantId, phoneNumber);
                if (user == null)
                {
                    return new AbpLoginResult<Tenant, User>(AbpLoginResultType.InvalidUserNameOrEmailAddress, tenant);
                }
                if (await UserManager.IsLockedOutAsync(user))
                {
                    return new AbpLoginResult<Tenant, User>(AbpLoginResultType.LockedOut, tenant, user);
                }
                if (!loggedInFromExternalSource)
                {
                    if (!await UserManager.CheckPasswordAsync(user, plainPassword))
                    {
                        if (shouldLockout)
                        {
                            if (await TryLockOutAsync(tenantId, user.Id))
                            {
                                return new AbpLoginResult<Tenant, User>(AbpLoginResultType.LockedOut, tenant, user);
                            }
                        }
                        return new AbpLoginResult<Tenant, User>(AbpLoginResultType.InvalidPassword, tenant, user);
                    }
                    await UserManager.ResetAccessFailedCountAsync(user);
                }
                return await CreateLoginByMobileResultAsync(user, tenant);
            }
        }
        /// <summary>
        /// 创建手机号登陆结果
        /// </summary>
        /// <param name="user">用户</param>
        /// <param name="tenant">租户</param>
        /// <returns></returns>
        protected virtual async Task<AbpLoginResult<Tenant, User>> CreateLoginByMobileResultAsync(User user,
            Tenant tenant = null)
        {
            //用户未激活
            if (!user.IsActive)
                //返回用户未激活
                return new AbpLoginResult<Tenant, User>(AbpLoginResultType.UserIsNotActive, tenant, user);
            //用户手机号未认证
            if (!user.IsPhoneNumberConfirmed)
                //返回用户手机号未认证
                return new AbpLoginResult<Tenant, User>(AbpLoginResultType.UserPhoneNumberIsNotConfirmed, tenant, user);
            user.LastLoginTime = Clock.Now;
            //更新用户
            await _userManager.UpdateAsync(user);
            await _unitOfWorkManager.Current.SaveChangesAsync();
            var principal = await _claimsPrincipalFactory.CreateAsync(user);
            return new AbpLoginResult<Tenant, User>(
                tenant,
                user,
                (ClaimsIdentity)principal.Identity
            );
        }

这就是添加手机号登录的代码,添加的相关的其他方法,参照手机验证码登录里面的代码.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值