Esempio n. 1
0
        public HttpResponseMessage Post([FromBody] TokenRequest request)
        {
            HttpResponseMessage response;

            if (ModelState.IsValid)
            {
                UserService service = new UserService();

                if (service.ValidateUser(request))
                {
                    User user = service.GetUser(request.UserName, request.Type);

                    var IdentityClaims = new List <Claim>()
                    {
                        new Claim(ClaimTypes.Name, user.Name),
                        new Claim(ClaimTypes.NameIdentifier, request.UserName),
                        new Claim(ClaimTypes.Role, request.Type),
                        new Claim(ClaimTypes.MobilePhone, user.MobileNumber)
                    };

                    AuthenticationModule authModule = new AuthenticationModule();
                    string authToken = authModule.GenerateTokenFromClaims(IdentityClaims);

                    LoginResponseDto dto = new LoginResponseDto()
                    {
                        AccessToken = authToken,
                        UserProfile = user
                    };

                    var responseObj = JsonConvert.SerializeObject(dto);

                    response         = Request.CreateResponse(HttpStatusCode.OK);
                    response.Content = new StringContent(responseObj, Encoding.UTF8, "application/json");
                }
                else
                {
                    response = Request.CreateResponse(HttpStatusCode.Unauthorized);
                }
            }
            else
            {
                response = Request.CreateResponse(HttpStatusCode.Unauthorized);
            }

            return(response);
        }
 public JWTAuthenticationFilter()
 {
     _authModule = new AuthenticationModule();
 }