Identity & SSOvendor:microsoftEmail OTPUnwrapped Magic LinkCarrier SMS DIDCloud KMS TOTP
Automate Microsoft 365 & Azure Entra ID 2FA Codes in Playwright & Stagehand
Automate Microsoft Entra ID, Azure, and M365 6–8 digit single-use security codes, SMS challenges, and TOTP authenticator flows.
1. 60-Second Gmail & Outlook Auto-Forwarding Filter
Don't want to change your existing account email on Microsoft 365 & Entra ID? Keep your corporate inbox on Microsoft 365 & Entra ID and forward only verification emails to <identity-slug>@in.sentryrelay.com. Sentry Relay's Stage 2 unwrapper preserves the original Microsoft 365 & Entra ID sender header automatically (full setup guide).
# Paste into Gmail -> Settings -> Filters -> Create a new filter ("Has the words")
from:(microsoft.com OR accountprotection.microsoft.com OR live.com OR office.com) ("security code" OR "verification code" OR "single-use code")
# Action: Forward it to -> <your-identity-slug>@in.sentryrelay.com2. Copy-Paste Microsoft 365 & Entra ID 2FA Automation Snippets
Choose your browser automation stack below. All snippets resolve codes in <200ms with Upstash Redis 15-second lookback protection so arrived-before-wait race conditions never fail a run.
import { test, expect } from '@playwright/test';
import { SentryRelay } from '@sentryrelay/sdk';
const relay = new SentryRelay({ apiKey: process.env.SENTRY_RELAY_API_KEY! });
test('Microsoft Entra ID / M365 login with Email security code or TOTP', async ({ page }) => {
await page.goto('https://login.microsoftonline.com/');
await page.fill('input[type="email"]', process.env.M365_EMAIL!);
await page.click('input[type="submit"]');
// If passwordless single-use email code is prompted
const otcInput = page.locator('input[name="otc"]');
if (await otcInput.isVisible({ timeout: 5000 }).catch(() => false)) {
const auth = await relay.wait('m365-bot', {
sender: 'microsoft',
timeout: 35,
});
await otcInput.fill(auth.event!.otpCode!);
await page.click('input[type="submit"]');
}
await expect(page).toHaveURL(/office\.com|portal\.azure\.com|microsoft/);
});3. Microsoft 365 & Entra ID Production Gotchas
7-Digit and 8-Digit Single-Use Codes
Unlike most SaaS tools that strictly use 6 digits, Microsoft routinely sends 7-digit or 8-digit single-use codes. Stage 4 captures `\d{6,8}` specifically for Microsoft.
Sender Subdomain Variance
Codes arrive from `accountprotection.microsoft.com`, `microsoft.com`, `live.com`, or `office.com`. Filtering on `sender: "microsoft"` (or omitting sender on a dedicated Identity) catches them reliably.
Disable Number-Matching Push on Bot Accounts
If Entra ID enforces Microsoft Authenticator number matching, add Software OATH TOTP or Email OTP as an authentication method for your automation service account.