Skip to content

Instantly share code, notes, and snippets.

@renatoeufe
Created November 10, 2017 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatoeufe/6f79d7484eb0952c6eb94d465ff69dee to your computer and use it in GitHub Desktop.
Save renatoeufe/6f79d7484eb0952c6eb94d465ff69dee to your computer and use it in GitHub Desktop.
Exemplo de criação de usuários
using IdentityModel.Client;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace Client
{
public class Program
{
public static void Main(string[] args) => MainAsync().GetAwaiter().GetResult();
// <PackageReference Include="IdentityModel" Version="2.12.0" />
private static async Task MainAsync()
{
var disco = await DiscoveryClient.GetAsync("http://localhost:50751");
var tokenClient = new TokenClient(disco.TokenEndpoint, "clientcredentials-qa", "secret");
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("admin-api");
if (tokenResponse.IsError)
{
Console.WriteLine(tokenResponse.Error);
return;
}
Console.WriteLine(tokenResponse.Json);
Console.WriteLine("\n\n");
using (var client = new HttpClient())
{
client.SetBearerToken(tokenResponse.AccessToken);
client.BaseAddress = new Uri("http://localhost:50751/extensions/v1/");
var body = new StringContent(JsonConvert.SerializeObject(new
{
FirstName = "Nome",
LastName = "Sobrenome",
PhoneNumber = "Telefone",
UserName = "email@dominio.com.br"
}), Encoding.UTF8, "application/json");
var response = await client.PostAsync("users", body);
if (!response.IsSuccessStatusCode)
{
Console.WriteLine(response.StatusCode);
}
else
{
Console.WriteLine("OK");
}
}
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment