static async Task Main()
        {
            using var handler             = new HttpClientHandler();
            handler.UseDefaultCredentials = true;
            handler.ServerCertificateCustomValidationCallback =
                HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;

            using var ntHandler = new NegotiatedTokenHandler("/token", handler);
            using var client    = new HttpClient(ntHandler)
                  {
                      BaseAddress = new Uri("https://localhost:5001"),
                  };

            using (var resp = await client.GetAsync(new Uri("/unprotected", UriKind.RelativeOrAbsolute)))
            {
                Console.WriteLine("UNPROTECTED:");
                resp.EnsureSuccessStatusCode();
                Console.WriteLine(await resp.Content.ReadAsStringAsync());
                Console.WriteLine();
            }

            using (var resp = await client.GetAsync("/protected"))
            {
                Console.WriteLine("PROTECTED:");
                resp.EnsureSuccessStatusCode();
                Console.WriteLine(await resp.Content.ReadAsStringAsync());
                Console.WriteLine();
            }
        }
        static async Task Main()
        {
            using var httpHandler = new HttpClientHandler()
                  {
                      // For local dev and testing, we just use an untrusted
                      // self-signed cert so ignore those errors
                      ServerCertificateCustomValidationCallback =
                          HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,

                      // We need to let the client provide our
                      // default Win Auth credentials if prompted
                      UseDefaultCredentials = true,
                  };
            using var negtokHandler = new NegotiatedTokenHandler("/token", httpHandler);
            using var httpClient    = new HttpClient(negtokHandler);

            var grpcChannelOptions = new GrpcChannelOptions
            {
                HttpClient        = httpClient,
                DisposeHttpClient = false,
            };

            using var channel = GrpcChannel.ForAddress("https://localhost:5001", grpcChannelOptions);
            var client = new Greeter.GreeterClient(channel);

            var helloWorld = await client.SayHelloAsync(
                new HelloRequest { Name = "World" });

            Console.WriteLine(helloWorld.Message);

            var unprotectedReply = await client.GetUnprotectedDetailsAsync(
                new DetailsInput { KeepGroupSid = false });

            Console.WriteLine("UNPROTECTED:");
            Console.WriteLine(unprotectedReply.Details);
            Console.WriteLine();

            var protectedReply = await client.GetProtectedDetailsAsync(
                new DetailsInput { KeepGroupSid = false });

            Console.WriteLine("PROTECTED:");
            Console.WriteLine(protectedReply.Details);
            Console.WriteLine();
        }
コード例 #3
0
        public async Task <UpperReply> ToUpperByRpc(string input)
        {
            using var httpHandler = new HttpClientHandler()
                  {
                      // If the API is behind a self-signed cert (for testing and
                      // demonstration purposes) we need to ignore the TLS cert errors
                      ServerCertificateCustomValidationCallback =
                          HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
                      CheckCertificateRevocationList = false,

                      // Support any of the relatively recent TLS protos
                      SslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12,

                      PreAuthenticate       = true,
                      UseDefaultCredentials = true,
                  };
            using var negTokHandler = new NegotiatedTokenHandler(TokenPath, httpHandler);
            using var httpClient    = new HttpClient(negTokHandler);

            var grpcChannelOptions = new GrpcChannelOptions
            {
                HttpClient        = httpClient,
                DisposeHttpClient = false,
            };

            using var channel = GrpcChannel.ForAddress(ServerAddress, grpcChannelOptions);
            var client = new Greeter.GreeterClient(channel);

            // var helloWorld = await client.SayHelloAsync(
            //     new HelloRequest { Name = "World" });
            // Console.WriteLine("HelloWorldMessage: {0}", helloWorld.Message);
            // return helloWorld.Message;

            var upperReply = await client.ToUpperAsync(
                new UpperRequest { Input = input });

            Console.WriteLine("Got RPC response:"
                              + $" [{System.Text.Json.JsonSerializer.Serialize(upperReply)}]");

            return(upperReply);
        }