Exemplo n.º 1
0
        //TODO: Do poprawy, ma zwracac responsemessage z tokenem i informacjami
        public string Login(string email, string password)
        {
            var player = new Player {
                Email = email
            };

            var responseMessage = playerDal.Get(player);

            if (responseMessage.GetResponseCode() != Resources.Enums.ResponseStatusCodes.Successfully)
            {
                return(null);
            }

            if (!VerifyPassword(password, responseMessage.GetObject().PasswordHash, responseMessage.GetObject().HashSalt))
            {
                return(null);
            }

            myPlayer = responseMessage.GetObject();
            // authentication successful so generate jwt token
            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(_appSettings.Secret);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, responseMessage.GetObject().Id.ToString())
                }),
                Expires            = DateTime.UtcNow.AddHours(5),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token = tokenHandler.CreateToken(tokenDescriptor);

            return(tokenHandler.WriteToken(token));
        }
Exemplo n.º 2
0
 public UserService(IOptions <AppSettings> appSettings, IIdentity userIdentity)
 {
     _appSettings = appSettings.Value;
     playerDal    = new PlayerDal();
     if (userIdentity != null)
     {
         messageDal = new MessageDal(int.Parse(userIdentity.Name));
         myPlayer   = playerDal.Get(new Player {
             Id = int.Parse(userIdentity.Name)
         }).GetObject();
     }
 }