Skip to content

Any examples using a managed identity for authentication? #34

@bzbetty

Description

@bzbetty

Attempt to use a managed identity but getting a 401, was hoping there was a guide or sample that could do this.

Have it working using a standard token (although took a long time to realise my api version was using the old 2023 preview format instead of the 2025-09-01 one).

here's my rough method of trying to get the token, i'm assuming this either doesn't work at all or I'm guessing it for the wrong subject or similar.

	TokenCredential credential;
			if (string.IsNullOrEmpty(managedIdentityClientId))
			{
				throw new Exception("Managed identity client ID is not configured.");
			}

			_logger.LogInformation("Using user-assigned managed identity with client ID: {ClientId}", managedIdentityClientId);
			credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions
			{
				ManagedIdentityClientId = managedIdentityClientId
			});
			
			// Get access token for Azure Playwright Testing service
			// The scope for Azure Playwright Testing is: https://playwright.microsoft.com/.default
			var tokenRequestContext = new TokenRequestContext(new[] { "https://playwright.microsoft.com/.default" });
			var accessToken = await credential.GetTokenAsync(tokenRequestContext, CancellationToken.None);
		
		var wsEndpoint = $"{serviceUrl}?runId={Uri.EscapeDataString(runId)}&os={os}&api-version={apiVersion}";
		_logger.LogInformation($"Access token: {accessToken.Token}");
		
		var options = new BrowserTypeConnectOptions
		{
			Timeout = 3 * 60 * 1000, // 3 minutes
			Headers = new Dictionary<string, string>
			{
				{ "Authorization", $"Bearer {accessToken.Token}" }
			},
			ExposeNetwork = "<loopback>", // Use loopback to expose network
		};

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions