You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/grpc/authn-and-authz.md
+79-64Lines changed: 79 additions & 64 deletions
Original file line number
Diff line number
Diff line change
@@ -73,9 +73,10 @@ public bool DoAuthenticatedCall(
73
73
}
74
74
```
75
75
76
-
Configuring `ChannelCredentials` on a channel is an alternative way to send the token to the service with gRPC calls. A `ChannelCredentials` can include `CallCredentials`, which provide a way to automatically set `Metadata`.
76
+
Configuring `ChannelCredentials` on a channel is an alternative way to send the token to the service with gRPC calls. A `ChannelCredentials` can include `CallCredentials`, which provide a way to automatically set `Metadata`.`CallCredentials` is run each time a gRPC call is made, which avoids the need to write code in multiple places to pass the token yourself.
77
77
78
-
`CallCredentials` is run each time a gRPC call is made, which avoids the need to write code in multiple places to pass the token yourself. Note that `CallCredentials` are only applied if the channel is secured with TLS. `CallCredentials` aren't applied on unsecured non-TLS channels.
78
+
> [!NOTE]
79
+
> `CallCredentials` are only applied if the channel is secured with TLS. Sending authentication headers over an insecure connection has security implications and shouldn't be done in production environments. An app can configure a channel to ignore this behavior and always use `CallCredentials` by setting `UnsafeUseInsecureChannelCallCredentials` on a channel.
79
80
80
81
The credential in the following example configures the channel to send the token with every gRPC call:
gRPC client factory can create clients that send a bearer token using `ChannelCredentials`. When configuring a client, assign the `CallCredentials` the client should use with the `ConfigureChannel` method.
105
+
gRPC client factory can create clients that send a bearer token using `AddCallCredentials`. This method is available in [Grpc.Net.ClientFactory](https://www.nuget.org/packages/Grpc.Net.ClientFactory) version 2.46.0 or later.
106
+
107
+
The delegate passed to `AddCallCredentials` is executed for each gRPC call:
A gRPC interceptor can also be used to configure a bearer token. An advantage to using an interceptor is the client factory can be configured to create a new interceptor for each client. This allows an interceptor to be[constructed from DI using scoped and transient services](/dotnet/core/extensions/dependency-injection#service-lifetimes).
125
+
Dependency injection (DI) can be combined with `AddCallCredentials`. An overload passes `IServiceProvider` to the delegate, which can be used to get a service[constructed from DI using scoped and transient services](/dotnet/core/extensions/dependency-injection#service-lifetimes).
130
126
131
127
Consider an app that has:
128
+
132
129
* A user-defined `ITokenProvider` for getting a bearer token. `ITokenProvider` is registered in DI with a scoped lifetime.
133
130
* gRPC client factory is configured to create clients that are injected into gRPC services and Web API controllers.
134
131
* gRPC calls should use `ITokenProvider` to get a bearer token.
* Defines `AuthInterceptor` which is constructed using the user defined `ITokenProvider`.
171
+
The preceding code:
172
+
173
+
* Defines `ITokenProvider` and `AppTokenProvider`. These types handle resolving the authentication token for gRPC calls.
174
+
* Registers the `AppTokenProvider` type with DI in a scoped lifetime. `AppTokenProvider` caches the token so that only the first call in the scope is required to calculate it.
168
175
* Registers the `GreeterClient` type with client factory.
169
-
* Configures the `AuthInterceptor` for this client using `InterceptorScope.Client`. A new interceptor is created for each client instance. When a client is created for a gRPC service or Web API controller, the scoped `ITokenProvider`is injected into the interceptor.
176
+
* Configures `AddCallCredentials` for this client. The delegate is executed each time a call is made and adds the token returned by `ITokenProvider`to the metadata.
gRPC client factory can create clients that send a bearer token using `ChannelCredentials`. When configuring a client, assign the `CallCredentials` the client should use with the `ConfigureChannel` method.
370
+
gRPC client factory can create clients that send a bearer token using `AddCallCredentials`. This method is available in [Grpc.Net.ClientFactory](https://www.nuget.org/packages/Grpc.Net.ClientFactory) version 2.46.0 or later.
371
+
372
+
The delegate passed to `AddCallCredentials` is executed for each gRPC call:
A gRPC interceptor can also be used to configure a bearer token. An advantage to using an interceptor is the client factory can be configured to create a new interceptor for each client. This allows an interceptor to be[constructed from DI using scoped and transient services](/dotnet/core/extensions/dependency-injection#service-lifetimes).
390
+
Dependency injection (DI) can be combined with `AddCallCredentials`. An overload passes `IServiceProvider` to the delegate, which can be used to get a service[constructed from DI using scoped and transient services](/dotnet/core/extensions/dependency-injection#service-lifetimes).
387
391
388
392
Consider an app that has:
393
+
389
394
* A user-defined `ITokenProvider` for getting a bearer token. `ITokenProvider` is registered in DI with a scoped lifetime.
390
395
* gRPC client factory is configured to create clients that are injected into gRPC services and Web API controllers.
391
396
* gRPC calls should use `ITokenProvider` to get a bearer token.
* Defines `AuthInterceptor` which is constructed using the user defined `ITokenProvider`.
436
+
The preceding code:
437
+
438
+
* Defines `ITokenProvider` and `AppTokenProvider`. These types handle resolving the authentication token for gRPC calls.
439
+
* Registers the `AppTokenProvider` type with DI in a scoped lifetime. `AppTokenProvider` caches the token so that only the first call in the scope is required to calculate it.
425
440
* Registers the `GreeterClient` type with client factory.
426
-
* Configures the `AuthInterceptor` for this client using `InterceptorScope.Client`. A new interceptor is created for each client instance. When a client is created for a gRPC service or Web API controller, the scoped `ITokenProvider`is injected into the interceptor.
441
+
* Configures `AddCallCredentials` for this client. The delegate is executed each time a call is made and adds the token returned by `ITokenProvider`to the metadata.
For more information about configuring call credentials, see [Bearer token with gRPC client factory](xref:grpc/authn-and-authz#bearer-token-with-grpc-client-factory).
160
+
139
161
## Deadline and cancellation propagation
140
162
141
163
gRPC clients created by the factory in a gRPC service can be configured with `EnableCallContextPropagation()` to automatically propagate the deadline and cancellation token to child calls. The `EnableCallContextPropagation()` extension method is available in the [Grpc.AspNetCore.Server.ClientFactory](https://www.nuget.org/packages/Grpc.AspNetCore.Server.ClientFactory) NuGet package.
Copy file name to clipboardExpand all lines: aspnetcore/grpc/configuration.md
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -69,6 +69,7 @@ The following table describes options for configuring gRPC channels:
69
69
|`Credentials`|`null`| A `ChannelCredentials` instance. Credentials are used to add authentication metadata to gRPC calls. |
70
70
|`CompressionProviders`| gzip | A collection of compression providers used to compress and decompress messages. Custom compression providers can be created and added to the collection. The default configured providers support **gzip** compression. |
71
71
|`ThrowOperationCanceledOnCancellation`|`false`| If set to `true`, clients throw <xref:System.OperationCanceledException> when a call is canceled or its deadline is exceeded. |
72
+
|`UnsafeUseInsecureChannelCallCredentials`|`false`| If set to `true`, `CallCredentials` are applied to gRPC calls made by an insecure channel. Sending authentication headers over an insecure connection has security implications and shouldn't be done in production environments. |
72
73
|`MaxRetryAttempts`| 5 | The maximum retry attempts. This value limits any retry and hedging attempt values specified in the service config. Setting this value alone doesn't enable retries. Retries are enabled in the service config, which can be done using `ServiceConfig`. A `null` value removes the maximum retry attempts limit. For more information about retries, see <xref:grpc/retries>. |
73
74
|`MaxRetryBufferSize`| 16 MB | The maximum buffer size in bytes that can be used to store sent messages when retrying or hedging calls. If the buffer limit is exceeded, then no more retry attempts are made and all hedging calls but one will be canceled. This limit is applied across all calls made using the channel. A `null` value removes the maximum retry buffer size limit. |
74
75
|`MaxRetryBufferPerCallSize`| 1 MB | The maximum buffer size in bytes that can be used to store sent messages when retrying or hedging calls. If the buffer limit is exceeded, then no more retry attempts are made and all hedging calls but one will be canceled. This limit is applied to one call. A `null` value removes the maximum retry buffer size limit per call. |
0 commit comments