Skip to content

Instantly share code, notes, and snippets.

View frankhu-2021's full-sized avatar

Frank Hu frankhu-2021

View GitHub Profile
@frankhu-2021
frankhu-2021 / MSFTGraphSDKGetUsers
Last active January 1, 2019 01:20
MicroosftGraphSDK Get Users
using Microsoft.Graph;
static async Task getUsersUsingGraphServiceClient()
{
var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
{
requestMessage
.Headers
.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
return Task.FromResult(0);
@frankhu-2021
frankhu-2021 / AcquireAccessToken
Created November 4, 2018 02:03
Get the access token using ADAL .net library using an interactive login flow
using Microsoft.IdentityModel.Clients.ActiveDirectory;
static async Task getAccessToken()
{
int retryCount = 0;
bool retry = false;
authContext = new AuthenticationContext(authority + tenantID);
do
{
retry = false;
@frankhu-2021
frankhu-2021 / MainMethod for .net tutorial
Created November 4, 2018 01:53
MainMethod for .net tutorial
static void Main(string[] args)
{
getAccessToken().Wait();
// GetMembers utilizes HTTP Client, will print JSON Prettified in method
getMembers().Wait();
Console.WriteLine("\n Press Enter to exit the program \n");
Console.ReadLine();
}
@frankhu-2021
frankhu-2021 / PrettifyJWT
Last active November 4, 2018 01:47
Prettify JWT token to be readable
using System.IdentityModel.Tokens.Jwt;
static async Task prettyJWTPrint(String myToken)
{
//Assume the input is in a control called txtJwtIn,
//and the output will be placed in a control called txtJwtOut
var jwtHandler = new JwtSecurityTokenHandler();
var jwtInput = myToken;
String prettyPrint = "";
@frankhu-2021
frankhu-2021 / GetMembers
Last active November 4, 2018 02:35
Get 5 users from the Microsoft Graph API using ADAL .net
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Net.Http;
using System.Net.Http.Headers;
static async Task getMembers()
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
Console.WriteLine("\n \n Retrieving users {0}", DateTime.Now.ToString());
HttpResponseMessage response = await httpClient.GetAsync(resourceUri + "/v1.0/users?$top=5");
@frankhu-2021
frankhu-2021 / PrettifyJSON
Last active February 14, 2025 18:58
PrettyPrint JSON with .NET using Newtonsoft.Json
using Newtonsoft.Json;
public static string JsonPrettify(string json)
{
using (var stringReader = new StringReader(json))
using (var stringWriter = new StringWriter())
{
var jsonReader = new JsonTextReader(stringReader);
var jsonWriter = new JsonTextWriter(stringWriter) { Formatting = Formatting.Indented };
jsonWriter.WriteToken(jsonReader);
private static string resourceUri = "https://graph.microsoft.com"; //Insert your resource here, I will keep this as the MSFT Graph resource
private static string clientId = "<your-client-id>"; // Insert your Client ID here
private static string redirectUri = "<your-redirect-uri>"; // Insert your redirect URI here (Reply URL of AAD Application Registration)
private static string authority = "https://login.microsoftonline.com/";
private static string tenantID = "<your-tenant-id>"; // Insert Your Tenant ID here
private static AuthenticationContext authContext = null;
private static AuthenticationResult result = null;
private static HttpClient httpClient = new HttpClient();