1818
1919using System ;
2020using System . IO ;
21+ using System . Security . Cryptography . X509Certificates ;
2122using System . Text ;
2223using Grpc . Testing ;
24+ using Microsoft . AspNetCore . Authentication . Certificate ;
2325using Microsoft . AspNetCore . Builder ;
2426using Microsoft . AspNetCore . Http ;
27+ using Microsoft . Extensions . Configuration ;
2528using Microsoft . Extensions . DependencyInjection ;
2629using Microsoft . Extensions . Hosting ;
27- using Microsoft . Extensions . Logging ;
2830using Newtonsoft . Json ;
29- #if CLIENT_CERTIFICATE_AUTHENTICATION
30- using System . Security . Cryptography . X509Certificates ;
31- using Microsoft . AspNetCore . Authentication . Certificate ;
32- #endif
3331
3432namespace GrpcAspNetCoreServer
3533{
3634 public class Startup
3735 {
36+ private readonly IConfiguration _config ;
37+
38+ public Startup ( IConfiguration config )
39+ {
40+ _config = config ;
41+ }
42+
3843 public void ConfigureServices ( IServiceCollection services )
3944 {
4045 services . AddGrpc ( o =>
@@ -45,19 +50,18 @@ public void ConfigureServices(IServiceCollection services)
4550 services . AddSingleton < BenchmarkServiceImpl > ( ) ;
4651 services . AddControllers ( ) ;
4752
48- #if CLIENT_CERTIFICATE_AUTHENTICATION
49- services . AddAuthorization ( ) ;
50- services . AddAuthentication ( CertificateAuthenticationDefaults . AuthenticationScheme )
51- . AddCertificate ( options =>
52- {
53- // Not recommended in production environments. The example is using a self-signed test certificate.
54- options . RevocationMode = X509RevocationMode . NoCheck ;
55- options . AllowedCertificateTypes = CertificateTypes . All ;
56- } ) ;
57- #endif
58- #if GRPC_WEB
59- services . AddGrpcWeb ( o => o . GrpcWebEnabled = true ) ;
60- #endif
53+ bool . TryParse ( _config [ "enableCertAuth" ] , out var enableCertAuth ) ;
54+ if ( enableCertAuth )
55+ {
56+ services . AddAuthorization ( ) ;
57+ services . AddAuthentication ( CertificateAuthenticationDefaults . AuthenticationScheme )
58+ . AddCertificate ( options =>
59+ {
60+ // Not recommended in production environments. The example is using a self-signed test certificate.
61+ options . RevocationMode = X509RevocationMode . NoCheck ;
62+ options . AllowedCertificateTypes = CertificateTypes . All ;
63+ } ) ;
64+ }
6165 }
6266
6367 public void Configure ( IApplicationBuilder app , IHostApplicationLifetime applicationLifetime )
@@ -67,29 +71,34 @@ public void Configure(IApplicationBuilder app, IHostApplicationLifetime applicat
6771
6872 app . UseRouting ( ) ;
6973
70- #if CLIENT_CERTIFICATE_AUTHENTICATION
71- app . UseAuthentication ( ) ;
72- app . UseAuthorization ( ) ;
73- #endif
74+ bool . TryParse ( _config [ "enableCertAuth" ] , out var enableCertAuth ) ;
75+ if ( enableCertAuth )
76+ {
77+ app . UseAuthentication ( ) ;
78+ app . UseAuthorization ( ) ;
79+ }
7480
75- #if GRPC_WEB
76- app . UseGrpcWeb ( ) ;
77- #endif
81+ bool . TryParse ( _config [ "enableGrpcWeb" ] , out var enableGrpcWeb ) ;
82+
83+ if ( enableGrpcWeb )
84+ {
85+ app . UseGrpcWeb ( new GrpcWebOptions { DefaultEnabled = true } ) ;
86+ }
7887
7988 app . UseMiddleware < ServiceProvidersMiddleware > ( ) ;
8089
8190 app . UseEndpoints ( endpoints =>
8291 {
83- endpoints . MapGrpcService < BenchmarkServiceImpl > ( ) ;
92+ ConfigureAuthorization ( endpoints . MapGrpcService < BenchmarkServiceImpl > ( ) ) ;
8493
85- endpoints . MapControllers ( ) ;
94+ ConfigureAuthorization ( endpoints . MapControllers ( ) ) ;
8695
87- endpoints . MapGet ( "/" , context =>
96+ ConfigureAuthorization ( endpoints . MapGet ( "/" , context =>
8897 {
8998 return context . Response . WriteAsync ( "Benchmark Server" ) ;
90- } ) ;
99+ } ) ) ;
91100
92- endpoints . MapPost ( "/unary" , async context =>
101+ ConfigureAuthorization ( endpoints . MapPost ( "/unary" , async context =>
93102 {
94103 MemoryStream ms = new MemoryStream ( ) ;
95104 await context . Request . Body . CopyToAsync ( ms ) ;
@@ -108,8 +117,17 @@ public void Configure(IApplicationBuilder app, IHostApplicationLifetime applicat
108117
109118 ms . Seek ( 0 , SeekOrigin . Begin ) ;
110119 await ms . CopyToAsync ( context . Response . Body ) ;
111- } ) ;
120+ } ) ) ;
112121 } ) ;
113122 }
123+
124+ private void ConfigureAuthorization ( IEndpointConventionBuilder builder )
125+ {
126+ bool . TryParse ( _config [ "enableCertAuth" ] , out var enableCertAuth ) ;
127+ if ( enableCertAuth )
128+ {
129+ builder . RequireAuthorization ( ) ;
130+ }
131+ }
114132 }
115133}
0 commit comments