Skip to content

Instantly share code, notes, and snippets.

@ahelland
ahelland / HomeController.cs
Created March 29, 2017 09:35
API Playground - C#
[HttpPost]
public async Task<IActionResult> CSharp(Code code)
{
string output = code.csx;
var apiURL = "https://www.microsoft.com/net/api/code";
using (var client = new HttpClient())
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(apiURL));
@ahelland
ahelland / CSharp.cshtml
Created March 29, 2017 09:39
API Playground - loadGet Template
function loadGet() {
document.getElementById("csxEditor").innerHTML = "";
var strGetTemplate = [
'using System;',
'using System.Net.Http;',
'\npublic class HelloAPI',
'{',
'\tpublic static void Main()',
'\t{',
'\t\tusing (var client = new HttpClient())',
@ahelland
ahelland / run.csx
Created July 19, 2017 11:04
Ruter API - Azure Function
using System;
using System.IO;
using System.Threading;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Microsoft.Azure.Management.DataLake.Store;
using Microsoft.Azure.Management.DataLake.Store.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest.Azure.Authentication;
@ahelland
ahelland / job.sql
Created July 19, 2017 11:09
Ruter API - ADLA Job
REFERENCE ASSEMBLY [Newtonsoft.Json];
REFERENCE ASSEMBLY [Microsoft.Analytics.Samples.Formats];
DECLARE @out string="adl://contoso.azuredatalakestore.net/foo/delayedDepartures.csv";
@departure =
EXTRACT DataFrameRef string,
DatedVehicleJourneyRef string,
DestinationName string,
AimedArrivalTime string,
@ahelland
ahelland / SASTokenFunction01.csx
Created December 20, 2017 17:14
Azure Function for sending a simple son payload to an Azure Event Hub
using System;
using System.Text;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
public static void Run(string input, TraceWriter log)
{
var resourceUri = "contoso.servicebus.windows.net";
var keyName = "RootManageSharedAccessKey";
@ahelland
ahelland / APIMAuth.xml
Created December 20, 2017 17:18
Azure API Management Policy for validating jwt and rewriting authorization header to use a SAS Token
<inbound>
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized">
<openid-config url="https://login.microsoftonline.com/common/.well-known/openid-configuration" />
<audiences>
<audience>https://management.core.windows.net/</audience>
</audiences>
<required-claims />
</validate-jwt>
<set-header name="Authorization" exists-action="override">
<value>@{
@ahelland
ahelland / APIMFunction_01.csx
Created December 20, 2017 17:20
Azure Function for making an http request to an API behind Azure API Management
using System;
using System.Text;
using System.Net.Http;
using System.Net.Http.Headers;
public static void Run(string input, TraceWriter log)
{
log.Info($"C# manually triggered function called with input: {input}");
var apimUrl = " https://contosio.azure-api.net/foo/messages";
@ahelland
ahelland / APIMFunction_02.csx
Created December 20, 2017 17:23
Azure Function for acquiring a token from Azure AD, and subsequently use this for auth towards Azure API Management
using System;
using System.Text;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
public static void Run(string input, TraceWriter log)
{
log.Info($"C# manually triggered function called with input: {input}");
public class PowerBI
{
private static readonly string Username = "[email protected]";
private static readonly string Password = "Pa$$w0rd";
private static readonly string stsFqdn = "https://adfs.contoso.com";
private static readonly string ResourceUrl = "https://analysis.windows.net/powerbi/api";
private static readonly string ClientId = "guid-from-Azure-Portal";
private static readonly string ClientSecret = "secret-from-Azure-Portal";
private static readonly string ApiUrl = "https://api.powerbi.com/";
private static readonly string GroupId = "group-guid";
var tokenCredentials = new TokenCredentials(token.access_token, "Bearer");
// Create a Power BI Client object. It will be used to call Power BI APIs.
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
// Generate Embed Token.
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
var tokenResponse = await client.Dashboards.GenerateTokenInGroupAsync(GroupId, dashboardId,
generateTokenRequestParameters);
}