Reputation: 14018
I want to call a protected API using Microsoft.Identity.Web
ASP.NET Core package. It provides instructions on how to configure it in the case of client secrets and certificates:
It also briefly mentions that secret-less alternatives exist, like workload identity federation for Azure Kubernetes:
You can propose a collection of client credentials, including a credential-less solution like workload identity federation for Azure Kubernetes. Previous versions of Microsoft.Identity.Web expressed the client secret in a single property "ClientSecret" instead of "ClientCredentials". This is still supported for backwards compatibility but you cannot use both the "ClientSecret" property, and the "ClientCredentials" collection.
How can I configure the package to use secrets-less solutions? In my case, I want to use ClientAssertionCredential
based on ManagedIdentityCredential(managedIdentityClientId)
, but I can't find it in the docs.
Any ideas?
Upvotes: 1
Views: 38
Reputation: 22082
The ClientAssertionCredential is part of the Azure.Identity library and is suitable for achieving secret-less authentication when running on Azure resources with managed identities.
1. Packages
Azure.Identity;
Microsoft.Identity.Client;
2. Sample Code from MS official sample.
var managedIdentityClientId = "<Your-Managed-Identity-Client-Id>";
string audience = "api://test";
var miCredential = new ManagedIdentityCredential(managedIdentityClientId);
ClientAssertionCredential clientAssertionCredential = new(
"tenantId",
"appClientId",
async (token) =>
{
// fetch Managed Identity token for the specified audience
var tokenRequestContext = new Azure.Core.TokenRequestContext(new[] { $"{audience}/.default" });
var accessToken = await miCredential.GetTokenAsync(tokenRequestContext).ConfigureAwait(false);
return accessToken.Token;
});
Related link : Leveraging Microsoft.Identity.Web for Secret-less API Authentication in ASP.NET Core
Upvotes: 0