public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); UsuariosController usuariosController = new UsuariosController(); ResponseViewModel <Usuario> responseViewModel = new ResponseViewModel <Usuario>(); Task.Run(async() => { ResponseViewModel <Usuario> verificaAcesso = await usuariosController.Login(new LoginRequisicao(context.UserName, context.Password)); responseViewModel = verificaAcesso; }).Wait(); //if (FuncionariosSeguranca.Login(context.UserName, context.Password)) if (responseViewModel.Sucesso.Equals(true)) { var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim("sub", context.UserName)); identity.AddClaim(new Claim("role", "user")); context.Validated(identity); } else { context.SetError("acesso inválido", "As credenciais do usuário não conferem...."); return; } }
public async Task Validar_LoginUsuario() { //ARRANGE Usuario usuarioRegistrado = new Usuario { Id = 3, Correo = "*****@*****.**", Contrasenya = "registrado123", RolId = 2 }; UsuarioCreateDTO registradoDTO = new UsuarioCreateDTO { Correo = "*****@*****.**", Contrasenya = "registrado123", RolId = 2 }; var mockMapper = new MapperConfiguration(cfg => { cfg.AddProfile(new MappingProfile()); }); var mapper = mockMapper.CreateMapper(); var repository = new Mock <IUsuarioRepository>(); repository.Setup(x => x.Login(It.IsAny <Usuario>())).ReturnsAsync(usuarioRegistrado).Verifiable(); var controller = new UsuariosController(repository.Object, mapper); //ACT var actionResult = await controller.Login(registradoDTO); var result = actionResult.Result as CreatedAtRouteResult; var dto = result.Value as UsuarioDTO; //ASSERT Assert.Equal(registradoDTO.Correo, dto.Correo); }
public void Testing_Redirect_With_An_User_That_Dont_Exist() { UsuariosController controller = new UsuariosController(); RedirectToRouteResult result = controller.Login(nombre: "Javier", pass: "******") as RedirectToRouteResult; Assert.AreEqual("Login", result.RouteValues["action"]); Assert.AreEqual("Usuarios", result.RouteValues["controller"]); }