Skip to content
This repository was archived by the owner on Apr 9, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions docs/guides/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ stub = Helloworld::Greeter::Stub.new('localhost:50051', creds: creds)

```csharp
// Base case - No encryption/authentication
var channel = new Channel("localhost:50051", Credentials.Insecure);
var channel = new Channel("localhost:50051", ChannelCredentials.Insecure);
var client = new Greeter.GreeterClient(channel);
...

// With server authentication SSL/TLS
var credentials = new SslCredentials(File.ReadAllText("ca.pem")); // Load a CA file
var channel = new Channel("localhost:50051", credentials);
var channelCredentials = new SslCredentials(File.ReadAllText("roots.pem")); // Load a custom roots file.
var channel = new Channel("myservice.example.com", channelCredentials);
var client = new Greeter.GreeterClient(channel);
```

Expand Down Expand Up @@ -173,28 +173,49 @@ var scope = 'https://www.googleapis.com/auth/grpc-testing';

###Authenticating with Google (C#)

**Base case - No encryption/authentication**
```csharp
// Base case - No encryption/authentication
var channel = new Channel("localhost:50051", Credentials.Insecure);
var channel = new Channel("localhost:50051", ChannelCredentials.Insecure);
var client = new Greeter.GreeterClient(channel);
...
```

// Authenticating with Google
**Authenticate using JWT access token (recommended approach)**
```csharp
using Grpc.Auth; // from Grpc.Auth NuGet package
...
var sslCredentials = new SslCredentials(); // Loads publicly trusted roots.
var channel = new Channel("greeter.googleapis.com", sslCredentials);
// Loads Google Application Default Credentials with publicly trusted roots.
var channelCredentials = await GoogleGrpcCredentials.GetApplicationDefaultAsync();

var channel = new Channel("greeter.googleapis.com", channelCredentials);
var client = new Greeter.GreeterClient(channel);
...
```

**Authenticate using OAuth2 token (legacy approach)**
```csharp
using Grpc.Auth; // from Grpc.Auth NuGet package
...
string scope = "https://www.googleapis.com/auth/grpc-testing";
var googleCredential = await GoogleCredential.GetApplicationDefaultAsync();
if (googleCredential.IsCreateScopedRequired)
{
googleCredential = googleCredential.CreateScoped(new[] { scope });
}
var channel = new Channel("greeter.googleapis.com", googleCredential.ToChannelCredentials());
var client = new Greeter.GreeterClient(channel);
...
```

**Authenticate a single RPC call**
```csharp
var channel = new Channel("greeter.googleapis.com", new SslCredentials()); // Use publicly trusted roots.
var client = new Greeter.GreeterClient(channel);
// Auth headers will be added to each call made by this client
client.HeaderInterceptor = AuthInterceptors.FromCredential(googleCredential);
...
var googleCredential = await GoogleCredential.GetApplicationDefaultAsync();
var result = client.SayHello(request, new CallOptions(credentials: googleCredential.ToCallCredentials()));
...

```

###Authenticating with Google (PHP)
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/basic/csharp.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ To call service methods, we first need to create a *stub*.
First, we need to create a gRPC client channel that will connect to gRPC server. Then, we use the `RouteGuide.NewClient` method of the `RouteGuide` class generated from our .proto.

```csharp
Channel channel = new Channel("127.0.0.1:50052", Credentials.Insecure)
var client = new RouteGuideClient(RouteGuide.NewClient(channel));
Channel channel = new Channel("127.0.0.1:50052", ChannelCredentials.Insecure)
var client = RouteGuide.NewClient(channel);

// YOUR CODE GOES HERE

Expand Down