public IActionResult Login([FromBody, Required]  VM.AccountLogin account)
        {
            //Get Hash PassWord
            var hashPassword = _account.GetByUserName(account.UserName)?.Password;

            //UserName ,PassWord Check
            if (hashPassword == null || !hashPassword.Equals(PublicFunction.GetHash(account.Password)))
            {
                return(Unauthorized());
            }

            //Add Log
            _accountLog.AddForse(_account.GetByUserName(account.UserName).Id, Public.Enums.AccountLogState.login);

            //Logined
            var secretKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("P@ssM0rdKeyAuthorization"));
            var signinCredenrials = new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256);
            var claimes           = new List <Claim>
            {
                new Claim(ClaimTypes.Name, account.UserName),
                new Claim(ClaimTypes.Role, "Manager")
            };

            var tokenOptions = new JwtSecurityToken(
                issuer: $"http://{this.Request.Host}",
                audience: $"http://{this.Request.Host}",
                claims: claimes,
                expires: DateTime.Now.AddMinutes(20),
                signingCredentials: signinCredenrials
                );

            var tokenString = new JwtSecurityTokenHandler().WriteToken(tokenOptions);

            return(Ok(new { Token = tokenString, Id = _account.GetByUserName(account.UserName).Id }));
        }
Example #2
0
 public static MM.Account Map([Required] AccountLogin entity) => new MM.Account()
 {
     UserName = entity.UserName, Password = entity.Password
 };