示例#1
0
        public IActionResult Login(LoginRequestDto request)
        {
            // sprawdzanie hasla w db
            string pass  = request.Passw;
            string index = request.IndexNumber;

            if (pass == null && index == null)
            {
                throw new Exception("Index number and password cannot be null.");
            }

            if (index == User.Identity.Name)
            {
            }
            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, index),
                new Claim(ClaimTypes.Name, index),
                new Claim(ClaimTypes.Role, "employee")
            };
            //var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["SecretKey"]));
            var key         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(pass));
            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var keystring = key.ToString();
            var salt      = Encrypt.CreateSalt();
            var encrypted = Encrypt.Create(keystring, salt);

            var token = new JwtSecurityToken(

                issuer: "SandCorp",
                audience: "Employees",
                claims: claims,
                expires: DateTime.Now.AddMinutes(10),
                signingCredentials: credentials

                );

            return(Ok(new
            {
                accessToken = new JwtSecurityTokenHandler().WriteToken(token),
                refreshToken = Guid.NewGuid()
            }));
        }