|
| 1 | +# Authenticators |
| 2 | + |
| 3 | +RestSharp includes authenticators for basic HTTP, OAuth1 and token-based (JWT and OAuth2). |
| 4 | + |
| 5 | +There are two ways to set the authenticator: client-wide or per-request. |
| 6 | + |
| 7 | +Set the client-wide authenticator by assigning the `Authenticator` property of `RestClientOptions`: |
| 8 | + |
| 9 | +```csharp |
| 10 | +var options = new RestClientOptions("https://example.com") { |
| 11 | + Authenticator = new HttpBasicAuthenticator("username", "password") |
| 12 | +}; |
| 13 | +var client = new RestClient(options); |
| 14 | +``` |
| 15 | + |
| 16 | +To set the authenticator per-request, assign the `Authenticator` property of `RestRequest`: |
| 17 | + |
| 18 | +```csharp |
| 19 | +var request = new RestRequest("/api/users/me") { |
| 20 | + Authenticator = new HttpBasicAuthenticator("username", "password") |
| 21 | +}; |
| 22 | +var response = await client.ExecuteAsync(request, cancellationToken); |
| 23 | +``` |
| 24 | + |
| 25 | +## Basic authentication |
| 26 | + |
| 27 | +The `HttpBasicAuthenticator` allows you pass a username and password as a basic `Authorization` header using a base64 encoded string. |
| 28 | + |
| 29 | +```csharp |
| 30 | +var options = new RestClientOptions("https://example.com") { |
| 31 | + Authenticator = new HttpBasicAuthenticator("username", "password") |
| 32 | +}; |
| 33 | +var client = new RestClient(options); |
| 34 | +``` |
| 35 | + |
| 36 | +## OAuth1 |
| 37 | + |
| 38 | +For OAuth1 authentication the `OAuth1Authenticator` class provides static methods to help generate an OAuth authenticator. |
| 39 | +OAuth1 authenticator will add the necessary OAuth parameters to the request, including signature. |
| 40 | + |
| 41 | +The authenticator will use `HMAC SHA1` to create a signature by default. |
| 42 | +Each static function to create the authenticator allows you to override the default and use another method to generate the signature. |
| 43 | + |
| 44 | +### Request token |
| 45 | + |
| 46 | +Getting a temporary request token is the usual first step in the 3-legged OAuth1 flow. |
| 47 | +Use `OAuth1Authenticator.ForRequestToken` function to get the request token authenticator. |
| 48 | +This method requires a `consumerKey` and `consumerSecret` to authenticate. |
| 49 | + |
| 50 | +```csharp |
| 51 | +var options = new RestClientOptions("https://api.twitter.com") { |
| 52 | + Authenticator = OAuth1Authenticator.ForRequestToken(consumerKey, consumerSecret) |
| 53 | +}; |
| 54 | +var client = new RestClient(options); |
| 55 | +var request = new RestRequest("oauth/request_token"); |
| 56 | +``` |
| 57 | + |
| 58 | +The response should contain the token and the token secret, which can then be used to complete the authorization process. |
| 59 | +If you need to provide the callback URL, assign the `CallbackUrl` property of the authenticator to the callback destination. |
| 60 | + |
| 61 | +### Access token |
| 62 | + |
| 63 | +Getting an access token is the usual third step in the 3-legged OAuth1 flow. |
| 64 | +This method retrieves an access token when provided `consumerKey`, `consumerSecret`, `oauthToken`, and `oauthTokenSecret`. |
| 65 | +If you don't have a token for this call, you need to make a call to get the request token as described above. |
| 66 | + |
| 67 | +```csharp |
| 68 | +var authenticator = OAuth1Authenticator.ForAccessToken( |
| 69 | + consumerKey, consumerSecret, oauthToken, oauthTokenSecret |
| 70 | +); |
| 71 | +var options = new RestClientOptions("https://api.twitter.com") { |
| 72 | + Authenticator = authenticator |
| 73 | +}; |
| 74 | +var client = new RestClient(options); |
| 75 | +var request = new RestRequest("oauth/access_token"); |
| 76 | +``` |
| 77 | + |
| 78 | +If the second step in 3-leg OAuth1 flow returned a verifier value, you can use another overload of `ForAccessToken`: |
| 79 | + |
| 80 | +```csharp |
| 81 | +var authenticator = OAuth1Authenticator.ForAccessToken( |
| 82 | + consumerKey, consumerSecret, oauthToken, oauthTokenSecret, verifier |
| 83 | +); |
| 84 | +``` |
| 85 | + |
| 86 | +The response should contain the access token that can be used to make calls to protected resources. |
| 87 | + |
| 88 | +For refreshing access tokens, use one of the two overloads of `ForAccessToken` that accept `sessionHandle`. |
| 89 | + |
| 90 | +### Protected resource |
| 91 | + |
| 92 | +When the access token is available, use `ForProtectedResource` function to get the authenticator for accessing protected resources. |
| 93 | + |
| 94 | +```csharp |
| 95 | +var authenticator = OAuth1Authenticator.ForAccessToken( |
| 96 | + consumerKey, consumerSecret, accessToken, accessTokenSecret |
| 97 | +); |
| 98 | +var options = new RestClientOptions("https://api.twitter.com/1.1") { |
| 99 | + Authenticator = authenticator |
| 100 | +}; |
| 101 | +var client = new RestClient(options); |
| 102 | +var request = new RestRequest("statuses/update.json", Method.Post) |
| 103 | + .AddParameter("status", "Hello Ladies + Gentlemen, a signed OAuth request!") |
| 104 | + .AddParameter("include_entities", "true"); |
| 105 | +``` |
| 106 | + |
| 107 | +### xAuth |
| 108 | + |
| 109 | +xAuth is a simplified version of OAuth1. It allows sending the username and password as `x_auth_username` and `x_auth_password` request parameters and directly get the access token. xAuth is not widely supported, but RestSharp still allows using it. |
| 110 | + |
| 111 | +Create an xAuth authenticator using `OAuth1Authenticator.ForClientAuthentication` function: |
| 112 | + |
| 113 | +```csharp |
| 114 | +var authenticator = OAuth1Authenticator.ForClientAuthentication( |
| 115 | + consumerKey, consumerSecret, username, password |
| 116 | +); |
| 117 | +``` |
| 118 | + |
| 119 | +### 0-legged OAuth |
| 120 | + |
| 121 | +The access token authenticator can be used in 0-legged OAuth scenarios by providing `null` for the `consumerSecret`. |
| 122 | + |
| 123 | +```csharp |
| 124 | +var authenticator = OAuth1Authenticator.ForAccessToken( |
| 125 | + consumerKey, null, oauthToken, oauthTokenSecret |
| 126 | +); |
| 127 | +``` |
| 128 | + |
| 129 | +## OAuth2 |
| 130 | + |
| 131 | +RestSharp has two very simple authenticators to send the access token as part of the request. |
| 132 | + |
| 133 | +`OAuth2UriQueryParameterAuthenticator` accepts the access token as the only constructor argument, and it will send the provided token as a query parameter `oauth_token`. |
| 134 | + |
| 135 | +`OAuth2AuthorizationRequestHeaderAuthenticator` has two constructors. One only accepts a single argument, which is the access token. The other constructor also allows you to specify the token type. The authenticator will then add an `Authorization` header using the specified token type or `OAuth` as the default token type, and the token itself. |
| 136 | + |
| 137 | +For example: |
| 138 | + |
| 139 | +```csharp |
| 140 | +var authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator( |
| 141 | + token, "Bearer" |
| 142 | +); |
| 143 | +var options = new RestClientOptions("https://example.com") { |
| 144 | + Authenticator = authenticator |
| 145 | +}; |
| 146 | +var client = new RestClient(options); |
| 147 | +``` |
| 148 | + |
| 149 | +The code above will tell RestSharp to send the bearer token with each request as a header. Essentially, the code above does the same as the sample for `JwtAuthenticator` below. |
| 150 | + |
| 151 | +As those authenticators don't do much to get the token itself, you might be interested in looking at our [sample OAuth2 authenticator](../usage/example.md#authenticator), which requests the token on its own. |
| 152 | + |
| 153 | +## JWT |
| 154 | + |
| 155 | +The JWT authentication can be supported by using `JwtAuthenticator`. It is a very simple class that can be constructed like this: |
| 156 | + |
| 157 | +```csharp |
| 158 | +var authenticator = new JwtAuthenticator(myToken); |
| 159 | +var options = new RestClientOptions("https://example.com") { |
| 160 | + Authenticator = authenticator |
| 161 | +}; |
| 162 | +var client = new RestClient(options); |
| 163 | +``` |
| 164 | + |
| 165 | +For each request, it will add an `Authorization` header with the value `Bearer <your token>`. |
| 166 | + |
| 167 | +As you might need to refresh the token from, you can use the `SetBearerToken` method to update the token. |
| 168 | + |
| 169 | +## Custom authenticator |
| 170 | + |
| 171 | +You can write your own implementation by implementing `IAuthenticator` and |
| 172 | +registering it with your RestClient: |
| 173 | + |
| 174 | +```csharp |
| 175 | +var authenticator = new SuperAuthenticator(); // implements IAuthenticator |
| 176 | +var options = new RestClientOptions("https://example.com") { |
| 177 | + Authenticator = authenticator |
| 178 | +}; |
| 179 | +var client = new RestClient(options); |
| 180 | +``` |
| 181 | + |
| 182 | +The `Authenticate` method is the very first thing called upon calling `RestClient.Execute` or `RestClient.Execute<T>`. |
| 183 | +It gets the `RestRequest` currently being executed giving you access to every part of the request data (headers, parameters, etc.) |
| 184 | + |
| 185 | +You can find an example of a custom authenticator that fetches and uses an OAuth2 bearer token [here](../usage/example.md#authenticator). |
0 commit comments