Exemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] RegisterModel model)
        {
            var captcha = await _captchaManager.GetCaptchaAsync(model.PhoneNumber, "register");

            if (captcha == null)
            {
                return(BadResult(ErrorCode.InvalidCaptcha));
            }
            if (captcha.ExpiredDate <= DateTimeOffset.Now)
            {
                return(BadResult(ErrorCode.CaptchExpired));
            }
            if (!captcha.Code.Equals(model.Captcha, StringComparison.OrdinalIgnoreCase))
            {
                return(BadResult(ErrorCode.InvalidCaptcha));
            }

            var user = new User();

            user.UserName             = model.UserName;
            user.NickName             = model.UserName;
            user.Email                = model.Email;
            user.PhoneNumber          = model.PhoneNumber;
            user.PhoneNumberConfirmed = true;
            //邀请码
            if (!string.IsNullOrEmpty(model.InviteKey))
            {
            }

            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                await Events.LogAsync(@event =>
                {
                    @event.UserId  = UserId;
                    @event.Message = Resources.Register_Success;
                }, EventType);

                return(OkResult());
            }
            return(BadResult(ErrorCode.RegisterFailured, result.ToErrorString()));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Post([FromBody] LoginModel model)
        {
            User user;

            if (model.Type.ToLower().Trim() == "account")
            {
                user = await _userManager.FindByNameAsync(model.UserName);

                if (user == null)
                {
                    return(BadResult(ErrorCode.InvalidUserNameOrPassword));
                }
                var result = await _userManager.PasswordSignInAsync(user, model.Password, model.AutoLogin);

                if (!result.Succeeded)
                {
                    return(BadResult(ErrorCode.InvalidUserNameOrPassword));
                }
                await Events.LogAsync(@event =>
                {
                    @event.UserId  = UserId;
                    @event.Message = Resources.Login_Account_Success;
                }, EventType);
            }
            else
            {
                user = await _userManager.FindByPhoneNumberAsync(model.Mobile);

                if (user == null)
                {
                    return(BadResult(ErrorCode.InvalidPhoneNumber));
                }
                var captcha = await _captchaManager.GetCaptchaAsync(model.Mobile, "login");

                if (captcha == null)
                {
                    return(BadResult(ErrorCode.InvalidCaptcha));
                }
                if (captcha.ExpiredDate <= DateTimeOffset.Now)
                {
                    return(BadResult(ErrorCode.CaptchExpired));
                }
                if (!captcha.Code.Equals(model.Captcha, StringComparison.OrdinalIgnoreCase))
                {
                    return(BadResult(ErrorCode.InvalidCaptcha));
                }
                await _userManager.SignInManager.SignInAsync(user, model.AutoLogin);

                await Events.LogAsync(@event =>
                {
                    @event.UserId  = UserId;
                    @event.Message = Resources.Login_Mobile_Success;
                }, EventType);
            }

            await _userManager.SetLoginStatusAsync(user);

            return(OkResult(new LoginResult
            {
                Type = model.Type,
                Token = GetToken(user),
                Authority = await _roleManager.GetAuthorityAsync(user.RoleId)
            }));
        }