Table of Contents

Class UsernamePasswordTokenProvider

Namespace
Arc4u.OAuth2.Token
Assembly
Arc4u.OAuth2.Client.dll

An ITokenProvider for a client application whose user signs in with a user name and password. The credentials are kept in the secure cache; when they are missing, they are requested from the registered IUserNamePasswordProvider. A token is requested from the CredentialDirect ICredentialTokenProvider and cached until it expires in less than one minute. The Authority and ServiceApplicationId settings are required; PasswordStoreKey selects the cache entries of the credentials (secret by default).

[Export("usernamePassword", typeof(ITokenProvider))]
public class UsernamePasswordTokenProvider : ISignOutTokenProvider, ITokenProvider
Inheritance
UsernamePasswordTokenProvider
Implements
Inherited Members

Constructors

UsernamePasswordTokenProvider(ISecureCache, INetworkInformation, ILogger<UsernamePasswordTokenProvider>, IServiceProvider)

An ITokenProvider for a client application whose user signs in with a user name and password. The credentials are kept in the secure cache; when they are missing, they are requested from the registered IUserNamePasswordProvider. A token is requested from the CredentialDirect ICredentialTokenProvider and cached until it expires in less than one minute. The Authority and ServiceApplicationId settings are required; PasswordStoreKey selects the cache entries of the credentials (secret by default).

public UsernamePasswordTokenProvider(ISecureCache secureCache, INetworkInformation networkStatus, ILogger<UsernamePasswordTokenProvider> logger, IServiceProvider container)

Parameters

secureCache ISecureCache

The secure cache holding the credentials and the token.

networkStatus INetworkInformation

The network status; a token cannot be requested without network.

logger ILogger<UsernamePasswordTokenProvider>

The logger.

container IServiceProvider

The service provider used to resolve the credential providers.

Fields

ProviderName

The key (usernamePassword) under which the provider is registered.

public const string ProviderName = "usernamePassword"

Field Value

string

Methods

CheckCredentialsAsync(string, string)

Checks credentials by requesting a token for them; on success the token is stored in the cache. It is the callback given to IUserNamePasswordProvider.

public Task<bool> CheckCredentialsAsync(string upn, string password)

Parameters

upn string

The user principal name.

password string

The password.

Returns

Task<bool>

true when a token has been obtained.

CreateBasicTokenInfoAsync(IKeyValueSettings, CredentialsResult)

Requests a token for the credentials from the CredentialDirect ICredentialTokenProvider.

protected Task<TokenInfo> CreateBasicTokenInfoAsync(IKeyValueSettings settings, CredentialsResult credential)

Parameters

settings IKeyValueSettings

The provider settings.

credential CredentialsResult

The credentials of the user.

Returns

Task<TokenInfo>

The token.

Exceptions

InvalidOperationException

The credential token provider is not registered or no token is received.

GetTokenAsync(IKeyValueSettings?, object?)

Gets the token of the user, asking for the credentials when they are not yet known.

public ValueTask<Result<TokenInfo>> GetTokenAsync(IKeyValueSettings? settings, object? platformParameters)

Parameters

settings IKeyValueSettings

The provider settings (see TokenKeys).

platformParameters object

Not used.

Returns

ValueTask<Result<TokenInfo>>

The token, or a failed result when there is no credential provider or the user gives no credential.

Exceptions

ArgumentNullException

settings is null.

ArgumentException

The Authority or ServiceApplicationId setting is missing.

InvalidOperationException

The settings are empty or no token is received.

SignOutAsync(IKeyValueSettings, CancellationToken)

Removes the user name, the password and the token of the user from the secure cache. Nothing is done when the settings have no Authority or no ServiceApplicationId: they are the settings of another provider.

public ValueTask SignOutAsync(IKeyValueSettings settings, CancellationToken cancellationToken)

Parameters

settings IKeyValueSettings

The provider settings.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

ValueTask

A completed task.

Exceptions

ArgumentNullException

settings is null.