Skip to content

Instantly share code, notes, and snippets.

# This script will require the Web Application and permissions setup in Azure Active Directory
$ClientID = "client id" # Should be a ~35 character string insert your info here
$ClientSecret = "secret # Should be a ~44 character string insert your info here
$loginURL = "https://login.microsoftonline.com"
$tenantdomain = "<tenantname>.onmicrosoft.com" # For example, contoso.onmicrosoft.com
$resource = "https://graph.microsoft.com"
# Get an Oauth 2 access token based on client id, secret and tenant domain
$body = @{grant_type="client_credentials";resource=$resource;client_id=$ClientID;client_secret=$ClientSecret}
$oauth = Invoke-RestMethod -Method Post -Uri $loginURL/$tenantdomain/oauth2/token -Body $body
@bachoang
bachoang / Startup.Auth.cs
Created July 20, 2019 17:56
How to set redirect URL for OpenID Connect middleware in MVC application
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
@bachoang
bachoang / OfficeDetection.cs
Created July 20, 2019 22:46
detect if the requests come from Office process
/*
Workaround below to detect Office request comes from the user smichtch in
https://github.com/aspnet/AspNetKatana/issues/78
*/
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
@bachoang
bachoang / Program.cs
Created August 11, 2019 04:45
MSAL.Net code to call Easy Auth Function App
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using System.Net.Http;
namespace MSALConsoleApp
{
@bachoang
bachoang / MSALNetB2C.cs
Last active August 28, 2019 00:57
MSAL.Net code to call a B2C application
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using System.Net.Http;
using System.Net;
namespace msalb2c
{
class Program
@bachoang
bachoang / Startup.cs
Created December 4, 2019 08:13
ASP.Net Core 2.2 Web API with AAD Authentication
using System.Collections.Generic;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace CoreWebAPIAAD
{
@bachoang
bachoang / Startup.cs
Created December 10, 2019 05:41
asp.net core web API implementing JwtBearerOptions events
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
@bachoang
bachoang / Program.cs
Created May 11, 2020 03:53
AppRoleAssignment using Azure SDK for .NET
using Microsoft.Azure.Management.Graph.RBAC.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using System;
namespace AzureSDKNetConsoleApp
{
class Program
{
static void Main(string[] args)
{
POST https://www.contoso.com/adfs/services/trust/2005/usernamemixed
HTTP Headers:
SOAPAction: http://schemas.xmlsoap.org/ws/2005/trust/RST/issue
Content-Type: application/soap+xml
client-request-id: 6ca4424a-8d29-4b62-b7cb-5e44ce038af5
return-client-request-id: true
Accept: application/json
@bachoang
bachoang / OAuth2PermissionGrant.ps1
Last active August 24, 2022 18:09
MS Graph PowerShell code to manage user consent
# script to manage User Consent (Delegated permission). The following script does
# - Remove all MS Graph Delegated permissions (if any) for the user
# - Perform user consent for an initial set of MS Graph permission
# - Update the consented permission list with some additional permissions
# - Remove some permissions from the consented permission list
# - Remove (revoke) all consented permissions for the user
# Continue to output logs - SilentlyContinue to keep going
$GLOBAL:DebugPreference="SilentlyContinue"