public IActionResult IncluirAlunoNoGrupo(Guid idGrupo, [FromBody] string email, [FromServices] UserManager <ApplicationUser> userManager, [FromServices] SignInManager <ApplicationUser> signInManager) { if (!ModelState.IsValid) { NotifyModelStateErrors(); return(Response <AlunoGrupoViewModel>()); } var accessToken = Request.Headers["Authorization"]; var handler = new JwtSecurityTokenHandler(); var securityToken = handler.ReadJwtToken(accessToken.ToString().Replace("Bearer ", "")); ApplicationUser userIdentity = _userManager.FindByNameAsync(email).Result; var aluno = _alunoAppService.GetByUserId(userIdentity.Id); var viewModel = new AlunoGrupoViewModel() { IdGrupo = idGrupo, IdAluno = aluno.Id, EmailAluno = email }; _grupoAppService.IncluirAluno(viewModel); return(Response <AlunoGrupoViewModel>(viewModel)); }
public async Task <IActionResult> Login([FromBody] LoginViewModel model, [FromServices] UserManager <ApplicationUser> userManager, [FromServices] SignInManager <ApplicationUser> signInManager, [FromServices] SigningConfigurations signingConfigurations, [FromServices] TokenConfigurations tokenConfigurations) { if (!ModelState.IsValid) { NotifyModelStateErrors(); return(Response <AlunoViewModel>(model)); } ApplicationUser userIdentity = null; bool credenciaisValidas = false; if (model != null && !String.IsNullOrWhiteSpace(model.Email)) { // Verifica a existência do usuário nas tabelas do // ASP.NET Core Identity userIdentity = userManager.FindByEmailAsync(model.Email).Result; if (userIdentity != null) { // Efetua o login com base no Id do usuário e sua senha var resultadoLogin = signInManager .CheckPasswordSignInAsync(userIdentity, model.Password, false) .Result; if (resultadoLogin.Succeeded) { // Verifica se o usuário em questão possui // a role Acesso-APIAlturas credenciaisValidas = userManager.IsInRoleAsync( userIdentity, "CANWRITEALUNODATA").Result; } } } if (credenciaisValidas) { ClaimsIdentity identity = new ClaimsIdentity( new GenericIdentity(model.Email, "Login"), new[] { new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString("N")), new Claim(JwtRegisteredClaimNames.UniqueName, model.Email) } ); DateTime dataCriacao = DateTime.Now; DateTime dataExpiracao = dataCriacao + TimeSpan.FromSeconds(tokenConfigurations.Seconds); var handler = new JwtSecurityTokenHandler(); var securityToken = handler.CreateToken(new SecurityTokenDescriptor { Issuer = tokenConfigurations.Issuer, Audience = tokenConfigurations.Audience, SigningCredentials = signingConfigurations.SigningCredentials, Subject = identity, NotBefore = dataCriacao, Expires = dataExpiracao }); var token = handler.WriteToken(securityToken); var aluno = _alunoAppService.GetByUserId(userIdentity?.Id); return(Response <AuthenticatedModel>(new AuthenticatedModel() { userId = userIdentity?.Id, alunoId = aluno.Id.ToString(), authenticated = true, created = dataCriacao.ToString("yyyy-MM-dd HH:mm:ss"), expiration = dataExpiracao.ToString("yyyy-MM-dd HH:mm:ss"), accessToken = token, message = "OK" })); } else { return(Response <AuthenticatedModel>(new { authenticated = false, message = "Falha ao autenticar" })); } return(Response <LoginViewModel>(model)); }