Ejemplo n.º 1
0
        public async Task <ChangePasswordResponseModel> ChangePassword(ChangePasswordRequestModel model)
        {
            var player = await GetPlayerById(model.PlayerId);

            if (player == null)
            {
                return new ChangePasswordResponseModel()
                       {
                           IsSuccess = false, BadPassword = false
                       }
            }
            ;

            var passwordSb = new StringBuilder();

            passwordSb.Append(_encrypter.Encrypted(model.Password));

            var salt = await _context.PlayerSalt.FirstOrDefaultAsync(t => t.PlayerId == model.PlayerId);

            passwordSb.Append(salt.Salt);

            if (passwordSb.ToString() != player.Password)
            {
                return new ChangePasswordResponseModel()
                       {
                           IsSuccess = false, BadPassword = true
                       }
            }
            ;

            var newPassword = _encrypter.Encrypted(model.NewPassword);

            player.Password = newPassword;

            player.PlayerHash = player.GetHashCode().ToString();

            var dbResult = _context.PlayerIdentity.Update(player);

            if (dbResult.State == EntityState.Modified)
            {
                await _context.SaveChangesAsync();

                return(new ChangePasswordResponseModel()
                {
                    IsSuccess = true, BadPassword = false
                });
            }

            return(new ChangePasswordResponseModel()
            {
                IsSuccess = false, BadPassword = false
            });
        }
        public async Task <UserLoginResponse> Login(UserLoginRequest userInfo)
        {
            if (userInfo.Login == null && userInfo.Password == null)
            {
                return(new UserLoginResponse {
                    PlayerId = -1, PlayerNickName = "unknown"
                });
            }

            var userTuple = await GetUserIdByLogin(userInfo.Login);

            if (userTuple.Item2 == -1)
            {
                return new UserLoginResponse {
                           PlayerId = -1, PlayerNickName = userTuple.Item1
                }
            }
            ;

            var isUserBanned = await _banService.CheckUserBan(userTuple.Item2);

            if (isUserBanned)
            {
                return new UserLoginResponse {
                           PlayerId = userTuple.Item2, PlayerNickName = userTuple.Item1, IsBanned = true
                }
            }
            ;

            StringBuilder sb = new StringBuilder(_encrypter.Encrypted(userInfo.Password));

            var salt = await GetSalt(userTuple.Item2);

            sb.Append(salt.Salt);

            var user = await GetUser(userInfo.Login, sb.ToString());

            if (user == null)
            {
                return new UserLoginResponse {
                           PlayerId = -1, PlayerNickName = "unknown"
                }
            }
            ;

            if (user.EmailConfirmed == false)
            {
                return new UserLoginResponse()
                       {
                           PlayerNickName = userTuple.Item1, EmailIsNotConfirmed = true
                       }
            }
            ;

            var tokenHandler = new JwtSecurityTokenHandler();
            var key          = Encoding.ASCII.GetBytes(this._applicationSettings.Secret);

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[] {
                    new Claim(ClaimTypes.Name, user.Id.ToString()),
                    new Claim(ClaimTypes.Role, Enum.GetName(typeof(RoleType), user.RoleType))
                }),
                Expires            = DateTime.UtcNow.AddDays(7),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };

            var token        = tokenHandler.CreateToken(tokenDescriptor);
            var encryptToken = tokenHandler.WriteToken(token);

            var lastDatePassMod = await GetLastDateModifiedPassword(userTuple.Item2);

            var ask = (DateTime.Today - lastDatePassMod).TotalDays > _applicationSettings.PasswordChangePeriod;

            // send email notification if is different ip
            return(new UserLoginResponse {
                PlayerId = user.Id, PlayerNickName = user.Nick, Token = encryptToken, GameToken = user.GameToken, AskAboutChangePassword = ask
            });
        }