Exemplo n.º 1
0
        private bool IsValidAuthentificateResponse(AuthenticateResponseDTO authentificateResponse)
        {
            if (!authentificateResponse.Name.Equals("Petar"))
            {
                return(false);
            }

            if (!authentificateResponse.Surname.Equals("Petrovic"))
            {
                return(false);
            }

            if (!authentificateResponse.Username.Equals("patient3"))
            {
                return(false);
            }

            if (authentificateResponse.Id != Guid.Parse("54455a55-094f-4081-89b3-757cafbd5ea1"))
            {
                return(false);
            }

            if (authentificateResponse.Token.Equals(""))
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        public void Login_when_give_valid_data()
        {
            LoginDTO loginDTO = this.GetValidLoginDTO();

            userRepository.GetAllPatients().Returns(this.GetListOfPatient());

            AuthenticateResponseDTO authentificateResponse = userService.Login(loginDTO);

            Assert.True(IsValidAuthentificateResponse(authentificateResponse));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Login(UserLogin model)
        {
            if (ModelState.IsValid)
            {
                var query = @"mutation($email: String!, $password: String!){
                              login(
                                input:{
                                  email: $email,
                                  password: $password
                                }
                              ){
                                token,
                                expiredToken,
                                id,
                                role,
                                email
                              }
                             }
                            ";

                var variable = new
                {
                    email    = model.Email,
                    password = model.Password
                };
                AuthenticateResponseDTO userLog = await new GraphqlConexionEntityHelper <AuthenticateResponseDTO>(_identityHelper).GraphqlConexionEntity(query, variable, true);

                if (userLog == null)
                {
                    TempData["Message"] = "Ingreso incorrecto";
                    TempData["Type"]    = "Error";
                    return(View(model));
                }
                var principal = _identityHelper.CreateIdentity(userLog.Id.ToString(), "", userLog);

                await HttpContext.SignInAsync("CookieAuthentication", principal);

                HttpContext.Session.SetString("JWToken", userLog.Token);

                TempData["Message"] = "Bienvenido a la Plataforma de Empleados";
                TempData["Type"]    = "Ok";
                return(RedirectToAction("Index", "Employee"));
            }
            else
            {
                TempData["Message"] = "Datos incorrectos";
                TempData["Type"]    = "Error";
                return(View(model));
            }
        }
Exemplo n.º 4
0
        public bool IsAuthenticated(AuthenticateRequestDTO request, out AuthenticateResponseDTO response)
        {
            response = new AuthenticateResponseDTO();
            var user = _userRepository.FindBy(u => u.Rfc == request.Rfc).SingleOrDefault();

            if (user == null)
            {
                throw new Exception("invalid credentials");
            }

            if (user.Password != request.Password)
            {
                throw new Exception("invalid credentials");
            }

            var claims = new[]
            {
                new Claim(ClaimTypes.Name, user.Rfc),
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())
            };


            var key         = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("YiQeYPB9qH5Yxb65AkfhS5w9YzJOCnZb"));
            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var now         = DateTime.Now;
            var expires     = DateTime.Now.AddSeconds(Convert.ToDouble(864000));

            var jwtToken = new JwtSecurityToken(
                issuer: "/konfio",
                audience: "konfio",
                claims: claims,
                expires: expires,
                signingCredentials: credentials
                );

            response.AccessToken = new JwtSecurityTokenHandler().WriteToken(jwtToken);
            response.TokenType   = JwtBearerDefaults.AuthenticationScheme;
            response.ExpiresIn   = Convert.ToInt32(jwtToken.Claims.ToList()[2].Value);
            response.UserId      = user.Id;

            return(true);
        }
Exemplo n.º 5
0
        public ClaimsPrincipal CreateIdentity(string Name, string Role, AuthenticateResponseDTO user)
        {
            var Claims = new Dictionary <string, string>();

            Claims.Add(ClaimTypes.NameIdentifier, user.Id.ToString());
            Claims.Add(ClaimTypes.Name, user.Token.ToString());
            Claims.Add(ClaimTypes.Role, user.Role.ToString());

            var identity = new ClaimsIdentity("EmployeePlatformKeySecret", Name, Role);

            identity.AddClaim(new Claim("UserName", Name));
            if (Claims != null)
            {
                foreach (var key in Claims.Keys)
                {
                    identity.AddClaim(new Claim(key, Claims[key]));
                }
            }

            return(new ClaimsPrincipal(identity));
        }
Exemplo n.º 6
0
        public async Task <AuthenticateResponseDTO> AuthenticationAsync(AuthenticateRequestDTO dto)
        {
            AuthenticateResponseDTO authenticateResponse = null;

            await TryCatchExtension.ExecuteAndHandleErrorAsync(
                async() =>
            {
                var login = await _authenticationManagementRepository
                            .LoginRepository
                            .FindAsync <Employee>(x => x.UserName == dto.UserName && x.Password == dto.Password.GetString(), x => x.Employee);

                var jwtToken = await GenerateJwtTokenAsync(login, GenerateClaimsIdentity(login.UserName, login.Id));

                authenticateResponse = AuthenticateResponseDTO.Create(login.Id, login.UserName, login.Employee.RoleId, jwtToken, true);
            },
                ex =>
            {
                _proLogger.Error($"An error occurred while authenticating the user having username: {dto.UserName}. Error: {ex.Message}");

                return(false);
            });

            return(authenticateResponse);
        }