Skip to content

Instantly share code, notes, and snippets.

View gscales's full-sized avatar

Glen Scales gscales

View GitHub Profile
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)
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 July 24, 2020 00:18
Exchange Online Remote Powershell MSAL ROPC
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 July 24, 2020 00:06
MSAL Exchange Online Powershell Interactive Logon
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 })
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;
@gscales
gscales / gist:1b7973f6fe194804270c9934a887b705
Created July 5, 2020 10:21
MFA Exchange Online Powershell
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
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;
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 June 1, 2020 23:38
Hybrid Modern Auth Invoke-webRequest
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 June 1, 2020 11:58
Powershell Modern Hybrid Authentication using MSAL
$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"