Skip to content

Instantly share code, notes, and snippets.

View frankhu-2021's full-sized avatar

Frank Hu frankhu-2021

View GitHub Profile
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();
@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);
@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 / 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 / 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 / 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 / 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 / CreateRandomUsers
Created November 4, 2018 03:10
This method will create users for a test tenant in order to test paging, or if you're trying to do some sort of testing.
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Net.Http.Headers;
using System.Net.Http;
static async Task createRandomUsers()
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
Console.WriteLine("\n \n Creating users {0}", DateTime.Now.ToString());
for(int index = 0;index < 10; ++index)
@frankhu-2021
frankhu-2021 / Regex for Phone numbers
Created January 6, 2019 01:44
Regex for Phone numbers
1. xxx-xxx-xxxx grep -o '[0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}' file.txt
2. (xxx)xxx-xxxx grep -o '([0-9]\{3\})[0-9]\{3\}\-[0-9]\{4\}' file.txt
3. xxx xxx xxxx grep -o '[0-9]\{3\}\s[0-9]\{3\}\s[0-9]\{4\}' file.txt
4. xxxxxxxxxx grep -o '[0-9]\{10\}' file.txt
grep -o '\([0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}\)\|\(([0-9]\{3\})[0-9]\{3\}\-[0-9]\{4\}\)\|\([0-9]\{10\}\)\|\([0-9]\{3\}\s[0-9]\{3\}\s[0-9]\{4\}\)' file.txt
- Found online, and put on my git for reference.
Credit goes to Raullen Chai
on
connect-azuread
$SPObjectID = "your-spn-objectid"
$role = (Get-AzureADDirectoryRole).objectId
For ($i=0; $i -lt $role.Count; $i++){
if(Get-AzureADDirectoryRoleMember -ObjectId $role.Get($i) | where {$_.objectId -eq $SPObjectID}) {
$role.DisplayName
}
}