Skip to content

Commit cc44c36

Browse files
committed
Fix authenticator docs
1 parent 2facbeb commit cc44c36

3 files changed

Lines changed: 53 additions & 17 deletions

File tree

docs/authenticators.md

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
# Authenticators
22

3-
RestSharp includes authenticators for basic HTTP (Authorization header),
4-
NTLM and parameter-based systems.
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("http://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+
```
524

625
## Basic Authentication
726

827
The `HttpBasicAuthenticator` allows you pass a username and password as a basic `Authorization` header using a base64 encoded string.
928

1029
```csharp
11-
var client = new RestClient("http://example.com");
12-
client.Authenticator = new HttpBasicAuthenticator("username", "password");
30+
var options = new RestClientOptions("http://example.com") {
31+
Authenticator = new HttpBasicAuthenticator("username", "password")
32+
};
33+
var client = new RestClient(options);
1334
```
1435

1536
## OAuth1
@@ -21,23 +42,29 @@ For OAuth1 authentication the `OAuth1Authenticator` class provides static method
2142
This method requires a `consumerKey` and `consumerSecret` to authenticate.
2243

2344
```csharp
24-
var client = new RestClient("http://example.com");
25-
client.Authenticator = OAuth1Authenticator.ForRequestToken(consumerKey, consumerSecret);
45+
var options = new RestClientOptions("http://example.com") {
46+
Authenticator = OAuth1Authenticator.ForRequestToken(consumerKey, consumerSecret)
47+
};
48+
var client = new RestClient(options);
2649
```
2750

2851
### Access token
2952

3053
This method retrieves an access token when provided `consumerKey`, `consumerSecret`, `oauthToken`, and `oauthTokenSecret`.
3154

3255
```csharp
33-
client.Authenticator = OAuth1Authenticator.ForAccessToken(
56+
var authenticator = OAuth1Authenticator.ForAccessToken(
3457
consumerKey, consumerSecret, oauthToken, oauthTokenSecret
3558
);
59+
var options = new RestClientOptions("http://example.com") {
60+
Authenticator = authenticator
61+
};
62+
var client = new RestClient(options);
3663
```
3764

3865
This method also includes an optional parameter to specify the `OAuthSignatureMethod`.
3966
```csharp
40-
client.Authenticator = OAuth1Authenticator.ForAccessToken(
67+
var authenticator = OAuth1Authenticator.ForAccessToken(
4168
consumerKey, consumerSecret, oauthToken, oauthTokenSecret,
4269
OAuthSignatureMethod.PlainText
4370
);
@@ -48,7 +75,7 @@ client.Authenticator = OAuth1Authenticator.ForAccessToken(
4875
The same access token authenticator can be used in 0-legged OAuth scenarios by providing `null` for the `consumerSecret`.
4976

5077
```csharp
51-
client.Authenticator = OAuth1Authenticator.ForAccessToken(
78+
var authenticator = OAuth1Authenticator.ForAccessToken(
5279
consumerKey, null, oauthToken, oauthTokenSecret
5380
);
5481
```
@@ -64,9 +91,13 @@ RestSharp has two very simple authenticators to send the access token as part of
6491
For example:
6592

6693
```csharp
67-
client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(
94+
var authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(
6895
token, "Bearer"
6996
);
97+
var options = new RestClientOptions("http://example.com") {
98+
Authenticator = authenticator
99+
};
100+
var client = new RestClient(options);
70101
```
71102

72103
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.
@@ -79,6 +110,10 @@ The JWT authentication can be supported by using `JwtAuthenticator`. It is a ver
79110

80111
```csharp
81112
var authenticator = new JwtAuthenticator(myToken);
113+
var options = new RestClientOptions("http://example.com") {
114+
Authenticator = authenticator
115+
};
116+
var client = new RestClient(options);
82117
```
83118

84119
For each request, it will add an `Authorization` header with the value `Bearer <your token>`.
@@ -91,10 +126,14 @@ You can write your own implementation by implementing `IAuthenticator` and
91126
registering it with your RestClient:
92127

93128
```csharp
94-
var client = new RestClient();
95-
client.Authenticator = new SuperAuthenticator(); // implements IAuthenticator
129+
var authenticator = new SuperAuthenticator(); // implements IAuthenticator
130+
var options = new RestClientOptions("http://example.com") {
131+
Authenticator = authenticator
132+
};
133+
var client = new RestClient(options);
96134
```
97135

98-
The `Authenticate` method is the very first thing called upon calling `RestClient.Execute` or `RestClient.Execute<T>`. The `Authenticate` method is passed the `RestRequest` currently being executed giving you access to every part of the request data (headers, parameters, etc.)
136+
The `Authenticate` method is the very first thing called upon calling `RestClient.Execute` or `RestClient.Execute<T>`.
137+
It gets the `RestRequest` currently being executed giving you access to every part of the request data (headers, parameters, etc.)
99138

100139
You can find an example of a custom authenticator that fetches and uses an OAuth2 bearer token [here](usage.md#authenticator).

docs/v107/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ request.AddHeader("Content-Type", "application/json");
6565
request.AddHeader("Accept", "application/json");
6666
```
6767

68-
This is completely unnecessary, and often harmful. The `Content-Type` header is the content header, not the request header. It might be different per individual part of the body when using multipart-form data, for example. RestSharp sets the correct content-type header automatically, based on your body format, so don't override it.
68+
This is **completely unnecessary**, and often harmful. The `Content-Type` header is the content header, not the request header. It might be different per individual part of the body when using multipart-form data, for example. RestSharp sets the correct content-type header automatically, based on your body format, so don't override it.
6969
The `Accept` header is set by RestSharp automatically based on registered serializers. By default, both XML and JSON are supported. Only change the `Accept` header if you need something else, like binary streams, or plain text.
7070

7171
### Making requests

src/RestSharp/RestClient.Extensions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,6 @@ public static async Task<RestResponse<T>> ExecuteAsync<T>(
102102
RestRequest request,
103103
CancellationToken cancellationToken = default
104104
) {
105-
if (request == null)
106-
throw new ArgumentNullException(nameof(request));
107-
108105
var response = await client.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
109106
return client.Serializers.Deserialize<T>(request, response, client.Options);
110107
}

0 commit comments

Comments
 (0)