Esempio n. 1
0
        public async Task <IActionResult> Login(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                User user = (User)await _loginService.Auth(model.Username, model.Password);

                if (user != null)
                {
                    var claims = new List <Claim> {
                        new Claim(ClaimTypes.Authentication, "true"),
                        new Claim(ClaimTypes.Sid, user.UserId.ToString()),
                        new Claim(ClaimTypes.Surname, user.Name),
                        new Claim(ClaimTypes.GivenName, user.Vorname),
                        new Claim(ClaimTypes.Name, user.Username)
                    };

                    var uroles = user.RoleToUser.Select(rtu => rtu.Role).Select(r => new Claim(ClaimTypes.Role, r.UserRoleType.ToString()));
                    foreach (var role in uroles)
                    {
                        claims.Add(role);
                    }


                    var claimsIdentity  = new ClaimsIdentity(claims, "password");
                    var claimsPrinciple = new ClaimsPrincipal(claimsIdentity);

                    await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrinciple, new AuthenticationProperties
                    {
                        ExpiresUtc   = DateTime.UtcNow.AddHours(12),
                        IsPersistent = false,
                        AllowRefresh = true
                    });

                    if (!string.IsNullOrEmpty(model.ReturnUrl) && Url.IsLocalUrl(model.ReturnUrl))
                    {
                        return(Redirect(model.ReturnUrl));
                    }
                    else
                    {
                        return(Redirect("~/"));
                    }
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Username or Password wrong!");
                    return(View(model));
                }
            }
            else
            {
                return(View(model));
            }
        }
Esempio n. 2
0
        public IActionResult Autentificar([FromBody] LoginRequest model)
        {
            var responses = _loginService.Auth(model);

            if (responses == null)
            {
                return(BadRequest("Usuario o contraseña incorrecta"));
            }
            else
            {
                return(Ok(responses));
            }
        }
Esempio n. 3
0
        public IActionResult Authenticate(LoginRequest request)
        {
            try
            {
                User user = loginService.Auth(request);
                if (user == null)
                {
                    return(BadRequest("Usuário ou senha inválidos no sistema"));
                }

                //Passo 5
                string token = CreateToken(user);

                return(Ok(new { token }));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }