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.
public class UsernamePasswordCredential : TokenCredential
- Inheritance
-
UsernamePasswordCredential
Constructors
UsernamePasswordCredential()
Protected constructor for mocking
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.
public UsernamePasswordCredential(string username, string password, string tenantId, string clientId)
Parameters
username
stringThe user account's username, also known as UPN.
password
stringThe user account's password.
tenantId
stringThe Microsoft Entra tenant (directory) ID or name.
clientId
stringThe 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.
public UsernamePasswordCredential(string username, string password, string tenantId, string clientId, TokenCredentialOptions options)
Parameters
username
stringThe user account's user name, UPN.
password
stringThe user account's password.
tenantId
stringThe Microsoft Entra tenant (directory) ID or name.
clientId
stringThe client (application) ID of an App Registration in the tenant.
options
TokenCredentialOptionsThe 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.
public UsernamePasswordCredential(string username, string password, string tenantId, string clientId, UsernamePasswordCredentialOptions options)
Parameters
username
stringThe user account's user name, UPN.
password
stringThe user account's password.
tenantId
stringThe Microsoft Entra tenant (directory) ID or name.
clientId
stringThe client (application) ID of an App Registration in the tenant.
options
UsernamePasswordCredentialOptionsThe client options for the newly created UsernamePasswordCredential
Methods
Authenticate(TokenRequestContext, CancellationToken)
Authenticates the user using the specified username and password.
public virtual AuthenticationRecord Authenticate(TokenRequestContext requestContext, CancellationToken cancellationToken = default)
Parameters
requestContext
TokenRequestContextThe details of the authentication request.
cancellationToken
CancellationTokenA CancellationToken controlling the request lifetime.
Returns
- AuthenticationRecord
The AuthenticationRecord of the authenticated account.
Authenticate(CancellationToken)
Authenticates the user using the specified username and password.
public virtual AuthenticationRecord Authenticate(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA CancellationToken controlling the request lifetime.
Returns
- AuthenticationRecord
The AuthenticationRecord of the authenticated account.
AuthenticateAsync(TokenRequestContext, CancellationToken)
Authenticates the user using the specified username and password.
public virtual Task<AuthenticationRecord> AuthenticateAsync(TokenRequestContext requestContext, CancellationToken cancellationToken = default)
Parameters
requestContext
TokenRequestContextThe details of the authentication request.
cancellationToken
CancellationTokenA CancellationToken controlling the request lifetime.
Returns
- Task<AuthenticationRecord>
The AuthenticationRecord of the authenticated account.
AuthenticateAsync(CancellationToken)
Authenticates the user using the specified username and password.
public virtual Task<AuthenticationRecord> AuthenticateAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA CancellationToken controlling the request lifetime.
Returns
- Task<AuthenticationRecord>
The AuthenticationRecord of the authenticated account.
GetToken(TokenRequestContext, CancellationToken)
Obtains a token for a user account, authenticating them using the given username and password. Note: This will fail with an AuthenticationFailedException if the specified user account has MFA enabled. Acquired tokens are cached by the credential instance. Token lifetime and refreshing is handled automatically. Where possible, reuse credential instances to optimize cache effectiveness.
public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken = default)
Parameters
requestContext
TokenRequestContextThe details of the authentication request.
cancellationToken
CancellationTokenA CancellationToken controlling the request lifetime.
Returns
- AccessToken
An Azure.Core.AccessToken which can be used to authenticate service client calls.
GetTokenAsync(TokenRequestContext, CancellationToken)
Obtains a token for a user account, authenticating them using the given username and password. Note: This will fail with an AuthenticationFailedException if the specified user account has MFA enabled. Acquired tokens are cached by the credential instance. Token lifetime and refreshing is handled automatically. Where possible, reuse credential instances to optimize cache effectiveness.
public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken = default)
Parameters
requestContext
TokenRequestContextThe details of the authentication request.
cancellationToken
CancellationTokenA CancellationToken controlling the request lifetime.
Returns
- ValueTask<AccessToken>
An Azure.Core.AccessToken which can be used to authenticate service client calls.