public async Task <IActionResult> CreateGoogle([FromBody] string idToken) { try { //Usa biblioteca para acessar o google e pegar o perfil GetProfile getProfile = new GetProfile(); //Classe com os campos de retorno do google ObjetoGoogle profile = await getProfile.Acessar(idToken); if (profile != null) { UsuarioModel model = new UsuarioModel { Email = profile.email, Nome = profile.name, Senha = Guid.NewGuid().ToString() }; var usuarioResult = await Create(model); //Faz o casting da resposta do metodo var aux = usuarioResult as CreatedAtActionResult; if (aux != null) { var tokenClient = new TokenClient("http://identityservertcc.azurewebsites.net/connect/token", "jarbasApp", "secret"); var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync(model.Email, model.Senha, "jarbasApi offline_access"); if (!tokenResponse.IsError) { var usuario = aux.Value as Usuario; var token = tokenResponse.Json; return(CreatedAtAction("Create", new { usuario, token })); } else { ModelState.AddModelError("Token", "O usuário foi criado, porém houve um erro no login, tente novamente"); return(BadRequest(ModelState.Values.SelectMany(e => e.Errors))); } } else { return(usuarioResult); } } else { ModelState.AddModelError("Google", "Erro ao obter informações do Google, tente novamente"); return(BadRequest(ModelState.Values.SelectMany(e => e.Errors))); } } catch (Exception e) { return(BadRequest(e.Message)); } }
public async Task ValidateAsync(ExtensionGrantValidationContext context) { var userToken = context.Request.Raw.Get("id_token"); if (string.IsNullOrEmpty(userToken)) { context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "Token não pode ser nulo"); return; } //Usa biblioteca para acessar o google e pegar o perfil GetProfile getProfile = new GetProfile(); //Classe com os campos de retorno do google ObjetoGoogle profile = await getProfile.Acessar(userToken); if (profile != null) { var usuario = await identityContext.Usuario.FirstOrDefaultAsync(u => u.Email.Equals(profile.email)); if (usuario != null) { context.Result = new GrantValidationResult(usuario.Id, "google"); return; } else { context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "Usuário não cadastrado"); return; } } else { context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "Erro ao obter informações do Google, tente novamente"); return; } }