Esempio n. 1
0
        public async Task <LoginResponse> Login([FromBody] LoginRequest loginRequest)
        {
            var response = new LoginResponse();

            try
            {
                if (loginRequest == null || (string.IsNullOrWhiteSpace(loginRequest.Username) || string.IsNullOrWhiteSpace(loginRequest.Password)))
                {
                    throw new APIException("Credentials cannot be null");
                }

                string         error    = string.Empty;
                ClaimsIdentity identity = await GetClaimsIdentity(loginRequest, out error);

                var jwtSecurityToken = await JwtTokenManager.GetJwtTokenForIdentity(loginRequest, identity);

                response.Token   = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);
                response.Expires = jwtSecurityToken.ValidTo;
                SaveResponseOfClient(response);
                response.IsSuccess    = true;
                response.ErrorMessage = error;
                response.Name         = identity.FindFirst("Name").Value;
            }
            catch (Exception ex)
            {
                response.IsSuccess    = false;
                response.ErrorMessage = "Login failed.";
                LogException(IdentityServer.API.EventIds.Login, ex);
            }

            return(response);
            //--validation
        }