Skip to content

Instantly share code, notes, and snippets.

View ianchursky's full-sized avatar

Ian Chursky ianchursky

View GitHub Profile
using System;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
namespace Whatever
{
public class ValidateJSONWebToken : ActionFilterAttribute
{
private DataContainer db = new DataContainer();
@ianchursky
ianchursky / AzureAppOnlyAccessToken.cs
Created April 23, 2018 20:21
Get an "app only" access token for Azure Active Directory applications...
string GetAppOnlyAccessToken(string tenantId, string clientId, string clientSecret)
{
string url = "https://login.microsoftonline.com/" + tenantId + "/oauth2/v2.0/token";
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Accept = "application/json; odata=verbose";
request.ContentType = "application/x-www-form-urlencoded";
string postData = "client_id=" + clientId + "&scope=https://graph.microsoft.com/.default&client_secret=" + clientSecret + "&grant_type=client_credentials";
byte[] postArray = System.Text.Encoding.UTF8.GetBytes(postData);
request.ContentLength = postArray.Length;
@ianchursky
ianchursky / AzureActiveDirectoryAuthentication.js
Last active April 25, 2017 22:35
Simple JavaScript based authentication/authorization for Azure Active Directory (AAD) applications
/***
Simple JavaScript based authentication/authorization for Azure Active Directory (AAD) applications
1. Register an app in Azure, make note of client ID (application ID) and redirect URL
2. Set Permissions.
Note: In order to get an access token for Office 365 API requests, your application will use the OAuth implicit grant flow. You need to update the application's manifest to allow the OAuth implicit grant flow because it is not allowed by default. Set oauth2AllowImplicitFlow to true in app manifest.
***/
@ianchursky
ianchursky / ZipFileToBinary.cs
Created August 11, 2016 03:56
Saving .zip file data to binary and back to .zip in C#
byte[] ConvertZipFileToBinary(HttpPostedFileBase zipfile)
{
try
{
byte[] data = new byte[zipfile.ContentLength];
zipfile.InputStream.Position = 0;
zipfile.InputStream.Read(data, 0, zipfile.ContentLength);
return data;
}
catch (Exception e)