Skip to content

Instantly share code, notes, and snippets.

Avatar

Glen Scales gscales

View GitHub Profile
View MailKit oAuth Example
String ClientId = "20773535-6b8f-4f3d-8f0e-4b7710d79afe";
string UserName = "user@domain.com";
string scope = "https://outlook.office.com/SMTP.Send";
string redirectUri = "msal20773535-6b8f-4f3d-8f0e-4b7710d79afe://auth";
string From = "Fromouser@domain.com;
String To = "Touser@domain.com";
String SMTPServer = "smtp.office365.com";
Int32 SMTPPort = 587;
PublicClientApplicationBuilder pcaConfig = PublicClientApplicationBuilder.Create(ClientId)
View gist:6ba458d3b5ae05e269d9f30457990a28
function Get-GraphUserPhoto {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $true)]
[String]
$Filename,
[Parameter(Position = 1, Mandatory = $true)]
[String]
$MailboxName,
[Parameter(Position = 2, Mandatory = $false)]
@gscales
gscales / gist:66cacbef64087a5c89587f1ce6a739fa
Created Jul 24, 2020
Exchange Online Remote Powershell MSAL ROPC
View gist:66cacbef64087a5c89587f1ce6a739fa
PSCredential pSCredential = new PSCredential("user@blah.onmicrosoft.com", new NetworkCredential("", "pass##").SecurePassword);
string MailboxName = pSCredential.UserName;
string scope = "https://outlook.office365.com/.default";
string ClientId = "a0c73c16-a7e3-4564-9a95-2bdf47383716";
HttpClient Client = new HttpClient();
var TenantId = ((dynamic)JsonConvert.DeserializeObject(Client.GetAsync("https://login.microsoftonline.com/" + MailboxName.Split('@')[1] + "/v2.0/.well-known/openid-configuration").Result.Content.ReadAsStringAsync().Result)).authorization_endpoint.ToString().Split('/')[3];
PublicClientApplicationBuilder pcaConfig = PublicClientApplicationBuilder.Create(ClientId);
pcaConfig.WithTenantId(TenantId);
@gscales
gscales / gist:d9d49fcab122f4327035b98cabfd1b41
Last active Jul 24, 2020
MSAL Exchange Online Powershell Interactive Logon
View gist:d9d49fcab122f4327035b98cabfd1b41
string MailboxName = "gscales@datarumble.com";
string scope = "https://outlook.office365.com/.default";
string ClientId = "a0c73c16-a7e3-4564-9a95-2bdf47383716";
PublicClientApplicationBuilder pcaConfig = PublicClientApplicationBuilder.Create(ClientId);
pcaConfig.WithAuthority(AadAuthorityAudience.AzureAdMultipleOrgs, false);
var TokenResult = pcaConfig.Build().AcquireTokenInteractive(new[] { scope })
View Basic Remote PowerShell Sample
System.Security.SecureString secureString = new System.Security.SecureString();
string myPassword = "password";
foreach (char c in myPassword)
secureString.AppendChar(c);
PSCredential credential = new PSCredential("glen@domain.com", secureString);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://ps.outlook.com/PowerShell-LiveID?PSVersion=2.0"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
connectionInfo.SkipCACheck = true;
connectionInfo.SkipCNCheck = true;
View gist:1b7973f6fe194804270c9934a887b705
Import-Module CredentialManager
Import-Module ./AzureMFAOTPv2.ps1
$Cred = Get-Credential
$token = Get-AccessTokenMFA -OTP (Get-TimeBasedOneTimePassword -SharedSecret (Get-StoredCredential -Target Auth.GS -AsCredentialObject).Password) -Credential $Cred -ClientId 'a0c73c16-a7e3-4564-9a95-2bdf47383716' -Scopes 'https://outlook.office365.com/.default'
$domain = $Cred.UserName.Split('@')[1]
$tokenValue = ConvertTo-SecureString "Bearer $($token.access_token)" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($Cred.UserName, $tokenValue)
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid?DelegatedOrg=$($domain)&BasicAuthToOAuthConversion=true" -Credential $credential -Authentication Basic -AllowRedirection
Import-PSSession $session
View EWS Proxy Modern Auth Sample
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using System.Net.Http;
using Microsoft.Identity.Client;
using Newtonsoft.Json;
using EWSWSDLoAuthExample.com.office365.outlook;
View Proxy code example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp23
{
class OAuthExchangeServiceBinding : com.office365.outlook.ExchangeServiceBinding
{
@gscales
gscales / gist:021becef1e16e34de43eea4b27615ec6
Created Jun 1, 2020
Hybrid Modern Auth Invoke-webRequest
View gist:021becef1e16e34de43eea4b27615ec6
function Show-OAuthWindow {
[CmdletBinding()]
param (
[System.Uri]
$Url
)
## Start Code Attribution
## Show-AuthWindow function is the work of the following Authors and should remain with the function if copied into other scripts
## https://foxdeploy.com/2015/11/02/using-powershell-and-oauth/
@gscales
gscales / gist:fe2fb8cbcaa95218b903c43a44e7cb5c
Created Jun 1, 2020
Powershell Modern Hybrid Authentication using MSAL
View gist:fe2fb8cbcaa95218b903c43a44e7cb5c
$MailboxName = "user@domain.com";
$body = @{
"username" = $MailboxName
}
$RealmDiscover = Invoke-RestMethod -Uri ("https://login.microsoftonline.com/common/GetCredentialType") -ContentType "application/json; charset=UTF-8" -Method POST -Body ($body | ConvertTo-Json)
if ([Int]$RealmDiscover.EstsProperties.DomainType -eq 1 -bor [Int32]$RealmDiscover.EstsProperties.DomainType -eq 2) {
throw "Not Office365 or hybrid"
}
else {
$AutoDiscoverURI = "https://outlook.office365.com/autodiscover/autodiscover.json/v1.0/" + $MailboxName + "?Protocol=EWS"
You can’t perform that action at this time.