Esempio n. 1
0
        public LoginResDto Authenticate(string username, string password)
        {
            var user = _context.Users.SingleOrDefault(x => x.Username == username && x.Password == password);

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

            var loginResDto = new LoginResDto();

            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(_appSettings.Secret);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Email, user.Id.ToString()),
                }),
                Expires            = DateTime.UtcNow.AddDays(7),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };

            var token = tokenHandler.CreateToken(tokenDescriptor);

            if (user.Role != null)
            {
                loginResDto.Role = user.Role;
            }
            loginResDto.Token    = tokenHandler.WriteToken(token);
            loginResDto.Username = user.Username;

            return(loginResDto);
        }
Esempio n. 2
0
        public async Task <IActionResult> Login(LoginReqDto loginReq)
        {
            var user = await uow.UserRepository.Authenticate(loginReq.UserName, loginReq.Password);

            if (user == null)
            {
                return(Unauthorized());
            }
            var loginRes = new LoginResDto();

            loginRes.UserName = user.Username;
            loginRes.Token    = "Token to be generated";
            return(Ok(loginRes));
        }
Esempio n. 3
0
        // http://localhost:5000/api/admin/login
        public async Task <IActionResult> Login(LoginReqDto loginReq)
        {
            var admin = await uow.AdminRepository.Authenticate(loginReq.UserName, loginReq.Password);

            if (admin == null)
            {
                return(Unauthorized());
            }
            var loginRes = new LoginResDto();

            loginRes.UserName = admin.Username;
            loginRes.Token    = CreateJWT(admin);
            return(Ok(loginRes));
        }
        public async Task <IActionResult> Login(LoginReqDto loginReq)
        {
            var user = await unitofWork.UserRepository.Authenticate(loginReq.Username, loginReq.Password);

            if (user == null)
            {
                return(Unauthorized());
            }
            var loginRes = new LoginResDto();

            loginRes.Username = user.Username;
            loginRes.Token    = CreateJWT(user);
            return(Ok(loginRes));
        }
Esempio n. 5
0
        public IActionResult Login(LoginDto loginDto)
        {
            var _loginResDto = _unitOfWork.UserRepository.Authenticate(loginDto.Username, loginDto.Password);

            if (_loginResDto == null)
            {
                return(Unauthorized());
            }

            var loginResDto = new LoginResDto();

            loginResDto.Username = _loginResDto.Username;
            loginResDto.Token    = _loginResDto.Token;
            loginResDto.Role     = _loginResDto.Role;

            return(Ok(loginResDto));
        }
Esempio n. 6
0
        public async Task <IActionResult> Login(LoginReqDto loginReq)
        {
            var user = await uow.UserRepository.Authenticate(loginReq.UserName, loginReq.Password);

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

            var loginResDto = new LoginResDto
            {
                UserName = user.Username,
                Token    = CreateJWT(user)
            };

            return(Ok(loginResDto));
        }
Esempio n. 7
0
        public async Task <IActionResult> Login(LoginReqDto loginReqDto)
        {
            var user = await uow.UserRepository.Authenticate(loginReqDto.Email, loginReqDto.Password);

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

            var loginRes = new LoginResDto();

            loginRes.Email     = user.Email;
            loginRes.FirstName = user.FirstName;
            loginRes.LastName  = user.LastName;
            loginRes.Mobile    = user.Mobile;
            loginRes.Token     = CreateJWT(user);

            return(Ok(loginRes));
        }
        public async Task <IActionResult> Login(LoginReqDto loginReq)
        {
            var user = await uow.UserRepository.Authenticate(loginReq.Username, loginReq.Password);

            ApiError apiError = new ApiError();

            if (user == null)
            {
                apiError.ErrorCode    = Unauthorized().StatusCode;
                apiError.ErrorMessage = "Invalid user name or password";
                return(Unauthorized(apiError));
            }

            var loginRes = new LoginResDto();

            loginRes.Username = loginReq.Username;
            loginRes.Token    = CreateJWT(user);
            return(Ok(loginRes));
        }
Esempio n. 9
0
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="request"></param>
        public ApiResult <LoginResDto> UserRegister(RegisterReqDto dto, HttpRequestMessage request)
        {
            string password = Tool.GetMD5(dto.Password);
            string ip       = Tool.GetIP();
            string regsql   = @"
  insert into UserInfo(UserName, Name, Password, Mobile, Coin, Money, Integral, SubTime, LastLoginTime, State,Pid,RegisterIP)
  values(@UserName, @Name, @Password, @Mobile, 0,0, 0, getdate(), getdate(), 0,@Pid,@RegisterIP);select @@identity ";

            SqlParameter[] regsp = new SqlParameter[]
            {
                new SqlParameter("@UserName", dto.Phone),
                new SqlParameter("@Name", dto.NickName),
                new SqlParameter("@Password", password),
                new SqlParameter("@Mobile", dto.Phone),
                new SqlParameter("@Pid", dto.InviteCode.HasValue ? dto.InviteCode.Value : 0),
                new SqlParameter("@RegisterIP", ip)
            };

            object obj = SqlHelper.ExecuteScalar(regsql, regsp);

            if (obj == null)
            {
                throw new ApiException(50000, "注册失败,请重试");
            }

            int userId = Convert.ToInt32(obj);

            var      couponModel = GetCoupon("A0001");
            DateTime beginTime   = DateTime.Now;
            DateTime endTime     = DateTime.Now.AddDays(couponModel.ExpiryDate);

            #region 注册时,添加一张查看卷
            UserCoupon uc = new UserCoupon();
            uc.UserId     = userId;
            uc.CouponCode = "A0001";
            uc.PlanId     = 0;
            uc.BeginTime  = beginTime;
            uc.EndTime    = endTime;
            uc.FromType   = 1;
            uc.State      = 1;
            AddUserCoupon(uc);
            #endregion

            //TODO:事务优化处理
            #region 发放邀请注册奖励
            if (dto.InviteCode.HasValue)
            {
                try
                {
                    var inviteUser = GetUserInfo(dto.InviteCode.Value);
                    if (inviteUser != null)
                    {
                        //受邀奖励
                        int myReward = GetRadomReward(3);
                        AddCoinReward(userId, inviteUser.Id.ToString(), 6, myReward, 1);
                        //邀请奖励
                        int upReward = GetRadomReward(1);
                        AddCoinReward((int)inviteUser.Id, userId.ToString(), 7, upReward, 1);
                        //添加邀请任务记录
                        AddUserTask((int)dto.InviteCode.Value, 105);

                        //上级的上级奖励
                        if (inviteUser.Pid.HasValue && inviteUser.Pid > 0)
                        {
                            var superUser = GetUserInfo(inviteUser.Pid.Value);

                            if (superUser != null)
                            {
                                int superReward = GetRadomReward(2);
                                AddCoinReward((int)superUser.Id, inviteUser.Id.ToString(), 7, superReward, userId);
                            }
                        }

                        #region 邀请注册时,邀请人添加一张查看卷
                        UserCoupon uc1 = new UserCoupon();
                        uc1.UserId     = (int)inviteUser.Id;
                        uc1.CouponCode = "A0001";
                        uc1.PlanId     = 0;
                        uc1.BeginTime  = beginTime;
                        uc1.EndTime    = endTime;
                        uc1.FromType   = 2;
                        uc1.State      = 1;
                        AddUserCoupon(uc1);
                        #endregion
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error("发放邀请注册奖励异常", ex);
                }
            }
            #endregion

            #region  发登录token
            string       webHost  = ConfigurationManager.AppSettings["webHost"];
            string       avater   = string.Format("{0}/images/default_avater.png", webHost);
            IdentityInfo authInfo = new IdentityInfo()
            {
                UserId      = userId,
                UserAccount = dto.Phone,
                UserStatus  = 0,
                UserName    = dto.NickName,
                IsTemp      = false,
                Avater      = avater
            };

            var tokenAuth = new QiuxunTokenAuthorizer(new ApiAuthContainer(request));
            tokenAuth.Authorize(authInfo);

            #endregion

            LoginResDto resDto = new LoginResDto()
            {
                Account  = dto.Phone,
                Avater   = avater,
                UserId   = userId,
                Mobile   = dto.Phone,
                NickName = dto.NickName
            };

            return(new ApiResult <LoginResDto>()
            {
                Data = resDto
            });
        }
Esempio n. 10
0
        public ApiResult <LoginResDto> Login(LoginReqDto reqDto, HttpRequestMessage request)
        {
            UserInfoService service     = new UserInfoService();
            UserInfo        accountInfo = service.GetFullUserInfoByMobile(reqDto.Account);

            if (accountInfo == null)
            {
                throw new ApiException(15023, "用户名不存在或密码错误");
            }

            if (accountInfo.Password.StartsWith("$2y"))
            {
                if (!Crypter.CheckPassword(reqDto.Password, accountInfo.Password))
                {
                    throw new ApiException(15023, "用户名不存在或密码错误");
                }
            }
            else
            {
                if (Tool.GetMD5(reqDto.Password) != accountInfo.Password)
                {
                    throw new ApiException(15023, "用户名不存在或密码错误");
                }
            }

            string webHost = ConfigurationManager.AppSettings["webHost"];
            string avater  = string.IsNullOrWhiteSpace(accountInfo.Avater)
                ? string.Format("{0}/images/default_avater.png", webHost)
                : accountInfo.Avater;

            LoginResDto resDto = new LoginResDto()
            {
                Account  = accountInfo.Mobile,
                Avater   = avater,
                UserId   = accountInfo.Id,
                Mobile   = accountInfo.Mobile,
                NickName = accountInfo.NickName
            };

            #region  发登录token

            IdentityInfo authInfo = new IdentityInfo()
            {
                UserId      = accountInfo.Id,
                UserAccount = accountInfo.Mobile,
                UserStatus  = (int)accountInfo.State,
                UserName    = accountInfo.NickName,
                IsTemp      = false,
                Avater      = avater
            };

            var tokenAuth = new QiuxunTokenAuthorizer(new ApiAuthContainer(request));
            tokenAuth.Authorize(authInfo);

            #endregion

            return(new ApiResult <LoginResDto>()
            {
                Data = resDto
            });
        }