Ejemplo n.º 1
0
        public ActionResult <string> PostMentors([FromBody] Mentor mentor)
        {
            if (authenticationRepository.AuthenticateMentor(mentor))
            {
                var claims = new[]
                {
                    new Claim(JwtRegisteredClaimNames.Sub, mentor.userName)
                };

                var signingKey = new SymmetricSecurityKey(
                    Encoding.UTF8.GetBytes("MyKeyForAuthentication"));

                var token = new JwtSecurityToken(
                    issuer: "iiht.com",
                    audience: "trainees",
                    expires: DateTime.Now.AddHours(1),
                    claims: claims,
                    signingCredentials:
                    new SigningCredentials(signingKey,
                                           SecurityAlgorithms.HmacSha256)
                    );

                return(Ok(new TokenResponse
                {
                    Token = new JwtSecurityTokenHandler().WriteToken(token),
                    Expiration = token.ValidTo
                }));
            }

            return(Unauthorized());
        }