Class UsernamePasswordCredential
Enables authentication to Microsoft Entra ID using a user's username and password. If the user has MFA enabled this credential will fail to get a token throwing an AuthenticationFailedException. Also, this credential requires a high degree of trust and is not recommended outside of prototyping when more secure credentials can be used.
Inheritance
Namespace: System.Dynamic.ExpandoObject
Assembly: Azure.Identity.dll
Syntax
[System.ComponentModel.EditorBrowsable]
[System.Obsolete("This credential is deprecated because it doesn't support multifactor authentication (MFA). See https://aka.ms/azsdk/identity/mfa for details about MFA enforcement for Microsoft Entra ID and migration guidance.")]
public class UsernamePasswordCredential : Azure.Core.TokenCredential
Constructors
UsernamePasswordCredential()
Protected constructor for mocking
Declaration
protected UsernamePasswordCredential ();
UsernamePasswordCredential(String, String, String, String)
Creates an instance of the UsernamePasswordCredential with the details needed to authenticate against Microsoft Entra ID with a simple username and password.
Declaration
public UsernamePasswordCredential (string username, string password, string tenantId, string clientId);
Parameters
System.String
username
The user account's username, also known as UPN. |
System.String
password
The user account's password. |
System.String
tenantId
The Microsoft Entra tenant (directory) ID or name. |
System.String
clientId
The client (application) ID of an App Registration in the tenant. |
UsernamePasswordCredential(String, String, String, String, TokenCredentialOptions)
Creates an instance of the UsernamePasswordCredential with the details needed to authenticate against Microsoft Entra ID with a simple username and password.
Declaration
public UsernamePasswordCredential (string username, string password, string tenantId, string clientId, Azure.Identity.TokenCredentialOptions options);
Parameters
System.String
username
The user account's user name, UPN. |
System.String
password
The user account's password. |
System.String
tenantId
The Microsoft Entra tenant (directory) ID or name. |
System.String
clientId
The client (application) ID of an App Registration in the tenant. |
TokenCredentialOptions
options
The client options for the newly created UsernamePasswordCredential |
UsernamePasswordCredential(String, String, String, String, UsernamePasswordCredentialOptions)
Creates an instance of the UsernamePasswordCredential with the details needed to authenticate against Microsoft Entra ID with a simple username and password.
Declaration
public UsernamePasswordCredential (string username, string password, string tenantId, string clientId, Azure.Identity.UsernamePasswordCredentialOptions options);
Parameters
System.String
username
The user account's user name, UPN. |
System.String
password
The user account's password. |
System.String
tenantId
The Microsoft Entra tenant (directory) ID or name. |
System.String
clientId
The client (application) ID of an App Registration in the tenant. |
UsernamePasswordCredentialOptions
options
The client options for the newly created UsernamePasswordCredential |
Methods
Authenticate(CancellationToken)
Authenticates the user using the specified username and password.
Declaration
public virtual Azure.Identity.AuthenticationRecord Authenticate (System.Threading.CancellationToken cancellationToken = null);
Parameters
System.Threading.CancellationToken
cancellationToken
A System.Threading.CancellationToken controlling the request lifetime. |
Returns
AuthenticationRecord
The AuthenticationRecord of the authenticated account. |
Authenticate(TokenRequestContext, CancellationToken)
Authenticates the user using the specified username and password.
Declaration
public virtual Azure.Identity.AuthenticationRecord Authenticate (Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken = null);
Parameters
Azure.Core.TokenRequestContext
requestContext
The details of the authentication request. |
System.Threading.CancellationToken
cancellationToken
A System.Threading.CancellationToken controlling the request lifetime. |
Returns
AuthenticationRecord
The AuthenticationRecord of the authenticated account. |
AuthenticateAsync(CancellationToken)
Authenticates the user using the specified username and password.
Declaration
[System.Diagnostics.DebuggerStepThrough]
public virtual System.Threading.Tasks.Task<Azure.Identity.AuthenticationRecord> AuthenticateAsync (System.Threading.CancellationToken cancellationToken = null);
Parameters
System.Threading.CancellationToken
cancellationToken
A System.Threading.CancellationToken controlling the request lifetime. |
Returns
System.Threading.Tasks.Task<AuthenticationRecord>
The AuthenticationRecord of the authenticated account. |
AuthenticateAsync(TokenRequestContext, CancellationToken)
Authenticates the user using the specified username and password.
Declaration
[System.Diagnostics.DebuggerStepThrough]
public virtual System.Threading.Tasks.Task<Azure.Identity.AuthenticationRecord> AuthenticateAsync (Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken = null);
Parameters
Azure.Core.TokenRequestContext
requestContext
The details of the authentication request. |
System.Threading.CancellationToken
cancellationToken
A System.Threading.CancellationToken controlling the request lifetime. |
Returns
System.Threading.Tasks.Task<AuthenticationRecord>
The AuthenticationRecord of the authenticated account. |
GetToken(TokenRequestContext, CancellationToken)
Obtains a token for a user account, authenticating them using the provided username and password. Acquired tokens are cached by the credential instance. Token lifetime and refreshing is handled automatically. Where possible, reuse credential instances to optimize cache effectiveness.
Declaration
public override Azure.Core.AccessToken GetToken (Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken = null);
Parameters
Azure.Core.TokenRequestContext
requestContext
The details of the authentication request. |
System.Threading.CancellationToken
cancellationToken
A System.Threading.CancellationToken controlling the request lifetime. |
Returns
Azure.Core.AccessToken
An Azure.Core.AccessToken which can be used to authenticate service client calls. |
Exceptions
AuthenticationFailedException
Thrown when the authentication failed, particularly if the specified user account has MFA enabled. |
GetTokenAsync(TokenRequestContext, CancellationToken)
Obtains a token for a user account, authenticating them using the provided username and password. Acquired tokens are cached by the credential instance. Token lifetime and refreshing is handled automatically. Where possible, reuse credential instances to optimize cache effectiveness.
Declaration
[System.Diagnostics.DebuggerStepThrough]
public override System.Threading.Tasks.ValueTask<Azure.Core.AccessToken> GetTokenAsync (Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken = null);
Parameters
Azure.Core.TokenRequestContext
requestContext
The details of the authentication request. |
System.Threading.CancellationToken
cancellationToken
A System.Threading.CancellationToken controlling the request lifetime. |
Returns
System.Threading.Tasks.ValueTask<Azure.Core.AccessToken>
An Azure.Core.AccessToken which can be used to authenticate service client calls. |
Exceptions
AuthenticationFailedException
Thrown when the authentication failed, particularly if the specified user account has MFA enabled. |