Movatterモバイル変換


[0]ホーム

URL:


Strawberry Shakev13

Authentication

To access a protected API with Strawberry Shake, you need to proof the user's identity to the server.Each network protocol of Strawberry Shake handles authentication a bit different.

HTTP

Strawberry Shake uses theHttpClientFactory to generate aHttpClient on every request.You can either register aHttpClient directly on theServiceCollection or use theConfigureHttpClient method on the client builder.

ConfigureHttpClient

The generated extension method to register the client on the service collection, returns a builder that can be used to configure the http client.

C#
services
.AddConferenceClient()
.ConfigureHttpClient(client =>
{
client.BaseAddress =
new Uri("https://workshop.chillicream.com/graphql/");
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", "Your Oauth token");
});

There is an overload of theConfigureHttpClient method that provides access to theIServiceProvider, in case the access token is stored there.

C#
services
.AddConferenceClient()
.ConfigureHttpClient((serviceProvider, client) =>
{
var token = serviceProvider.GetRequiredService<ISomeService>().Token;
});

The second parameter ofConfigureHttpClient allows direct access to theHttpClientBuilder. Use this delegate to register extensions like Polly.

C#
services
.AddConferenceClient()
.ConfigureHttpClient(
client => { /*...*/ },
builder => builder.AddPolly());

HttpClientFactory

In case you want to configure theHttpClient directly on theServiceCollection, Strawberry Shake generates you a propertyClientName, that you can use to set the correct name for the client.

C#
services.AddHttpClient(
ConferenceClient.ClientName,
client => client.BaseAddress =
new Uri("https://workshop.chillicream.com/graphql/"));
services.AddConferenceClient();

Websockets

There are three common ways to do authentication a request over a web socket. You can either specify the authentication headers, use cookies or send the access token with the first message over the socket.Similar to theHttpClient, you can configure the a web socket client over the client builder or theServiceCollection.

Strawberry Shake uses aIWebSocketClient that provides a similar interface as theHttpClient has.

ConfigureWebsocketClient

You can configure the web socket client directly on the client builder after you registered it on the service collection.

C#
services
.AddConferenceClient()
.ConfigureWebSocketClient(client =>
{
client.Uri = new Uri("ws://localhost:" + port + "/graphql");
client.Socket.Options.SetRequestHeader("Authorization", "Bearer ...");
});

You can also access theIServiceProvider with the following overload:

C#
services
.AddConferenceClient()
.ConfigureWebSocketClient((serviceProvider, client) =>
{
var token = serviceProvider.GetRequiredService<ISomeService>().Token;
});

The second parameter of theConfigureWebSocketClient method, can be used to access theIWebSocketClientBuilder

C#
services
.AddConferenceClient()
.ConfigureWebSocketClient(
(serviceProvider, client) =>
{
var token = serviceProvider.GetRequiredService<ISomeService>().Token;
},
builder =>
builder.ConfigureConnectionInterceptor<CustomConnectionInterceptor>());

WebSocketClientFactory

If you prefer to use theServiceCollection to configure your web socket, you can use theAddWebSocketClient method. Strawberry Shake generates aClientName property, on each client. You can use this, to easily specify the correct name of the client.

C#
services
.AddWebSocketClient(
ConferenceClient.ClientName,
client => client.Uri =
new Uri("wss://workshop.chillicream.cloud/graphql/"));
services.AddConferenceClient();

IWebSocketClient

On aIWebSocketClient you can configure theUri of your endpoint. You can also directly set aISocketConnectionInterceptor on the client, to intercept the connection and configure the initial payload. You do also have access to the underlyingClientWebSocket to configure headers or cookies.

C#
IWebSocketClient client;
client.Uri = new Uri("wss://workshop.chillicream.cloud/graphql/");
client.Socket.Options.SetRequestHeader("Authorization", "Bearer …");
client.ConnectionInterceptor = new CustomConnectionInterceptor();

Initial payload

In JavaScript it is not possible to add headers to a web socket. Therefor many GraphQL server do not use HTTP headers for the authentication of web sockets. Instead, they send the authentication token with the first payload to the server.

You can specify create this payload with aISocketConnectionInterceptor

C#
public class CustomConnectionInterceptor
: ISocketConnectionInterceptor
{
// the object returned by this method, will be included in the connection initialization message
public ValueTask<object?> CreateConnectionInitPayload(
ISocketProtocol protocol,
CancellationToken cancellationToken)
{
return new ValueTask<object?>(
new Dictionary<string, string> { ["authToken"] = "..." });
}
}

You can set the connection interceptor directly on theIWebSocketClient or on theIWebSocketClientBuilder.

Last updated on2025-06-16 byGlen
About this article
Help us improving our content
  1. Edit on GitHub
  2. Discuss on Slack

[8]ページ先頭

©2009-2025 Movatter.jp