Created
March 8, 2019 11:27
-
-
Save Tanver-Hasan/d52a21b01edcff52e2716837bc08f268 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 System; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
namespace ClientCredentials { | |
class Program { | |
private static string accessToken; | |
private static async Task Main (string[] args) { | |
await ClientCredentialsFlow (); | |
await GetUsers (); | |
// await CreateUser(); | |
} | |
protected static async Task ClientCredentialsFlow () { | |
var body = new Model { | |
grant_type = "client_credentials", | |
client_id = "[client id]", | |
client_secret = "[client secret]", | |
audience = "https://[domain].auth0.com/api/v2/" | |
}; | |
using (var client = new HttpClient ()) { | |
var content = JsonConvert.SerializeObject (body); | |
var stringContent = new StringContent (content, Encoding.UTF8, "application/json"); | |
var res = await client.PostAsync ("https://[domain].auth0.com/oauth/token", stringContent); | |
var responseBody = await res.Content.ReadAsStringAsync (); | |
var deserilizeBody = JsonConvert.DeserializeObject<AuthResponseModel> (responseBody); | |
accessToken = deserilizeBody.access_token; | |
Console.WriteLine (accessToken); | |
} | |
} | |
protected static async Task GetUsers () { | |
using (var client = new HttpClient ()) { | |
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue ("Bearer", accessToken); | |
var response = await client.GetAsync ("https://[domain].auth0.com/api/v2/users"); | |
var responseBody = await response.Content.ReadAsStringAsync (); | |
Console.WriteLine ("=============================="); | |
Console.WriteLine (responseBody); | |
} | |
} | |
internal class Model { | |
public string grant_type { get; set; } | |
public string client_id { get; set; } | |
public string client_secret { get; set; } | |
public string audience { get; set; } | |
} | |
internal class AuthResponseModel { | |
public string access_token { get; set; } | |
public string scopes { get; set; } | |
public string expires_in { get; set; } | |
public string token_type { get; set; } | |
} | |
internal class User { | |
public string email { get; set; } | |
public bool email_verified { get; set; } | |
public string connection { get; set; } | |
public string username { get; set; } | |
public string password { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment