Skip to content

Instantly share code, notes, and snippets.

# Run the below script in PowerShell Core
# to output json body for uploading SSL Cert to Enterprise Application
$pfxpath = "C:\Users\<path to pfx>\testpfx.pfx"
$cerpath = "C:\Users\<path to cer>\testcer.cer"
$password = "<pfx password>"
$Secure_String_Pwd = ConvertTo-SecureString $password -AsPlainText -Force
$pfx_cert = get-content $pfxpath -AsByteStream -Raw
@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"
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 / 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)
{
@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 / 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 / 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 / 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 / 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 / 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,