Created
November 10, 2017 21:44
-
-
Save renatoeufe/6f79d7484eb0952c6eb94d465ff69dee to your computer and use it in GitHub Desktop.
Exemplo de criação de usuários
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 protected]" | |
}), 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