#Authentication
By default, AutoRest generates clients that make unauthenticated HTTP requests. When the -AddCredentials flag is set to true
, the generated client will include a Credentials
property of type ServiceClientCredentials. The Microsoft.Rest.ClientRuntime package includes two ServiceClientCredentials :
TokenCredentials
- used for OAuth authentication.BasicAuthenticationCredentials
- used for basic username/password authentication.
Custom authentication behaviors can be implemented by inheriting from ServiceClientCredentials. The ProcessHttpRequestAsync()
method is invoked for each HTTP request.
In addition to using TokenCredentials
with raw OAuth token, a Microsoft.Rest.ClientRuntime.Azure.Authentication nuget package can be used.
Microsoft.Rest.ClientRuntime.Azure.Authentication
supports both username/password and service principal login scenarios.
Login using service principal and certificate
using Microsoft.Rest.Azure.Authentication;
...
X509Certificate2Collection certificate = <load cert>;
byte[] certificateAsBytes = certificate.Export(X509ContentType.Pkcs12, _certificatePassword);
ServiceClientCredentials creds = await ApplicationTokenProvider.LoginSilentAsync("<mydomain>", "<client_id>", certificateAsBytes, _certificatePassword);
Login using username and password
using Microsoft.Rest.Azure.Authentication;
...
ServiceClientCredentials creds = await UserTokenProvider.LoginSilentAsync("<client_id>", "<domain>", "<username>", "<password>");
Login with a prompt
using Microsoft.Rest.Azure.Authentication;
...
ServiceClientCredentials creds = await UserTokenProvider.LoginWithPromptAsync("<domain>", ActiveDirectoryClientSettings.UsePromptOnly("<client_id>", new Uri("urn:ietf:wg:oauth:2.0:oob")));