Nomirun SDK contains implementations for Client Credential Flow and ROPC (Resource Owner Password Credentials) for Keycloak
, IdentityServer4
and Microsoft Entra ID
.
Client Credential Flow and ROPC (Resource Owner Password Credentials) Flow are two different OAuth 2.0 authorization grant types used for authentication and authorization:
Keycloak support is limited to the client credential or Protection API token (PAT) flow in combination with token exchange. Additionally you can also get the token by using resource owner password credentials grant.
var auth = new KeycloakAuthenticator(options =>
{
options.AddClientCredentialFlowParameters(
new Uri("https://my.keycloakserver.com/auth/realms/realmX/protocol/openid-connect/token"),
"my_client",
"client_secret");
});
var token = await auth.GetAccessToken();
If you want to replace the PAT token with user token, you can additionally specify a username.
var auth = new KeycloakAuthenticator(options =>
{
options.AddClientCredentialFlowParameters(
new Uri("https://my.keycloakserver.com/auth/realms/realmX/protocol/openid-connect/token"),
"my_client",
"client_secret");
});
//Get client credentials flow access token
var token = await auth.GetAccessToken();
//Replace client credentials flow token for user access token
var userToken = await auth.ExchangeForUserToken("myuser@email.com");
var auth = new KeycloakAuthenticator(options =>
{
options.AddResourceOwnerPasswordCredentialFlowParameters(
new Uri("https://my.keycloakserver.com/auth/realms/realmX/protocol/openid-connect/token"),
"my_client",
"client_secret",
"user",
"pass");
});
var token = await auth.GetAccessToken();
Under the hood, it’s the same code that retrieves the client credentials flow access token, but authenticator is explicit for Identity Server 4. Additionally, you can also get the token by using resource owner password.
var auth = new IdentityServer4Authenticator(options =>
{
options.AddClientCredentialFlowParameters(
new Uri("https://<myserver>/token"),
"my_client"
"<client_secret>");
});
var token = await auth.GetAccessToken();
var auth = new IdentityServer4Authenticator(options =>
{
options.AddResourceOwnerPasswordCredentialFlowParameters(
new Uri("https://<myserver>/token"),
"my_client"
"<client_secret>",
"user",
"pass");
});
var token = await auth.GetAccessToken();
Under the hood, it’s the same code that retrieves the client credentials flow access token, but the authenticator is explicit for Azure B2C. Additionally, you can also get the token by using resource owner password credentials flow.
Azure B2C client credentials flow needs a defined scope which is usually https://graph.windows.net/.default.
var auth = new AzureB2CAuthenticator(options =>
{
options.AddClientCredentialFlowParameters(
new Uri("https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token"),
"<clientId>"
"<clientSecret>"
new string[] { "https://graph.windows.net/.default" });
});
var token = await auth.GetAccessToken();
var auth = new AzureB2CAuthenticator(options =>
{
options.AddResourceOwnerPasswordCredentialFlowParameters(
new Uri("https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token"),
"<clientId>"
"<clientSecret>"
new string[] { "https://graph.windows.net/.default" },
"user",
"pass");
});
var token = await auth.GetAccessToken();