コード例 #1
0
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <remarks>
        /// 用户通过VerifyPhoneNumber接口发送到手机的验证码,传递注册信息注册用户
        ///     如果没有此手机号的缓存记录或验证码不匹配,抛出验证码无效的异常
        ///     用户注册成功,清除缓存的验证码记录
        /// </remarks>
        public virtual async Task <IdentityUserDto> RegisterAsync(PhoneNumberRegisterDto input)
        {
            var phoneVerifyCacheKey = NormalizeCacheKey(input.PhoneNumber);

            var phoneVerifyCacheItem = await Cache.GetAsync(phoneVerifyCacheKey);

            if (phoneVerifyCacheItem == null || !phoneVerifyCacheItem.VerifyCode.Equals(input.VerifyCode))
            {
                throw new UserFriendlyException(L["PhoneVerifyCodeInvalid"]);
            }

            await CheckSelfRegistrationAsync();

            // 需要用户输入邮箱?
            //if (UserManager.Options.User.RequireUniqueEmail)
            //{
            //    if (input.EmailAddress.IsNullOrWhiteSpace())
            //    {
            //        throw new UserFriendlyException(L["RequiredEmailAddress"]);
            //    }
            //}

            var userEmail = input.EmailAddress ?? $"{input.PhoneNumber}@{CurrentTenant.Name ?? "default"}.io";//如果邮件地址不验证,随意写入一个
            var userName  = input.UserName ?? input.PhoneNumber;
            var user      = new IdentityUser(GuidGenerator.Create(), userName, userEmail, CurrentTenant.Id)
            {
                Name = input.Name ?? input.PhoneNumber
            };
            // 写入手机号要在创建用户之前,因为有一个自定义的手机号验证
            await UserStore.SetPhoneNumberAsync(user, input.PhoneNumber);

            await UserStore.SetPhoneNumberConfirmedAsync(user, true);

            (await UserManager.CreateAsync(user, input.Password)).CheckErrors();

            (await UserManager.AddDefaultRolesAsync(user)).CheckErrors();

            await Cache.RemoveAsync(phoneVerifyCacheKey);

            return(ObjectMapper.Map <IdentityUser, IdentityUserDto>(user));
        }
コード例 #2
0
 public virtual async Task <IdentityUserDto> RegisterAsync(PhoneNumberRegisterDto input)
 {
     return(await AccountAppService.RegisterAsync(input));
 }