@@ -18,13 +18,15 @@ namespace Microsoft.SCIM.WebHostSample
1818{
1919 public class Startup
2020 {
21+ private readonly IWebHostEnvironment _env ;
2122 private readonly IConfiguration _configuration ;
2223
2324 public IMonitor MonitoringBehavior { get ; set ; }
2425 public IProvider ProviderBehavior { get ; set ; }
2526
26- public Startup ( IConfiguration configuration )
27+ public Startup ( IWebHostEnvironment env , IConfiguration configuration )
2728 {
29+ this . _env = env ;
2830 this . _configuration = configuration ;
2931
3032 this . MonitoringBehavior = new ConsoleMonitor ( ) ;
@@ -35,26 +37,57 @@ public Startup(IConfiguration configuration)
3537 // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
3638 public void ConfigureServices ( IServiceCollection services )
3739 {
38- services . AddAuthentication ( options =>
40+ if ( _env . IsDevelopment ( ) )
3941 {
40- options . DefaultAuthenticateScheme = JwtBearerDefaults . AuthenticationScheme ;
41- options . DefaultAuthenticateScheme = JwtBearerDefaults . AuthenticationScheme ;
42- options . DefaultChallengeScheme = JwtBearerDefaults . AuthenticationScheme ;
43- } )
44- . AddJwtBearer ( options =>
42+ // Development environment code
43+ // Validation for bearer token for authorization used during testing.
44+ // This is not meant to replace proper OAuth for authentication purposes.
45+ services . AddAuthentication ( options =>
4546 {
46- options . TokenValidationParameters =
47- new TokenValidationParameters
47+ options . DefaultAuthenticateScheme = JwtBearerDefaults . AuthenticationScheme ;
48+ options . DefaultAuthenticateScheme = JwtBearerDefaults . AuthenticationScheme ;
49+ options . DefaultChallengeScheme = JwtBearerDefaults . AuthenticationScheme ;
50+ } )
51+ . AddJwtBearer ( options =>
52+ {
53+ options . TokenValidationParameters =
54+ new TokenValidationParameters
55+ {
56+ ValidateIssuer = false ,
57+ ValidateAudience = false ,
58+ ValidateLifetime = false ,
59+ ValidateIssuerSigningKey = false ,
60+ ValidIssuer = this . _configuration [ "Token:TokenIssuer" ] ,
61+ ValidAudience = this . _configuration [ "Token:TokenAudience" ] ,
62+ IssuerSigningKey = new SymmetricSecurityKey ( Encoding . UTF8 . GetBytes ( this . _configuration [ "Token:IssuerSigningKey" ] ) )
63+ } ;
64+ } ) ;
65+ }
66+ else
67+ {
68+ // Azure AD token validation code
69+ services . AddAuthentication ( options =>
70+ {
71+ options . DefaultAuthenticateScheme = JwtBearerDefaults . AuthenticationScheme ;
72+ options . DefaultAuthenticateScheme = JwtBearerDefaults . AuthenticationScheme ;
73+ options . DefaultChallengeScheme = JwtBearerDefaults . AuthenticationScheme ;
74+ } )
75+ . AddJwtBearer ( options =>
76+ {
77+ options . Authority = this . _configuration [ "Token:TokenIssuer" ] ;
78+ options . Audience = this . _configuration [ "Token:TokenAudience" ] ;
79+ options . Events = new JwtBearerEvents
4880 {
49- ValidateIssuer = false ,
50- ValidateAudience = false ,
51- ValidateLifetime = false ,
52- ValidateIssuerSigningKey = false ,
53- ValidIssuer = this . _configuration [ "Token:TokenIssuer" ] ,
54- ValidAudience = this . _configuration [ "Token:TokenAudience" ] ,
55- IssuerSigningKey = new SymmetricSecurityKey ( Encoding . UTF8 . GetBytes ( this . _configuration [ "Token:IssuerSigningKey" ] ) )
81+ OnTokenValidated = context =>
82+ {
83+ // NOTE: You can optionally take action when the OAuth 2.0 bearer token was validated.
84+
85+ return Task . CompletedTask ;
86+ } ,
87+ OnAuthenticationFailed = AuthenticationFailed
5688 } ;
57- } ) ;
89+ } ) ;
90+ }
5891
5992 services . AddControllers ( ) . AddNewtonsoftJson ( ) ;
6093
@@ -63,9 +96,9 @@ public void ConfigureServices(IServiceCollection services)
6396 }
6497
6598 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
66- public void Configure ( IApplicationBuilder app , IWebHostEnvironment env )
99+ public void Configure ( IApplicationBuilder app )
67100 {
68- if ( env . IsDevelopment ( ) )
101+ if ( _env . IsDevelopment ( ) )
69102 {
70103 app . UseDeveloperExceptionPage ( ) ;
71104 }
@@ -83,5 +116,16 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
83116 endpoints . MapDefaultControllerRoute ( ) ;
84117 } ) ;
85118 }
119+
120+ private Task AuthenticationFailed ( AuthenticationFailedContext arg )
121+ {
122+ // For debugging purposes only!
123+ var s = $ "{{AuthenticationFailed: '{ arg . Exception . Message } '}}";
124+
125+ arg . Response . ContentLength = s . Length ;
126+ arg . Response . Body . WriteAsync ( Encoding . UTF8 . GetBytes ( s ) , 0 , s . Length ) ;
127+
128+ return Task . FromException ( arg . Exception ) ;
129+ }
86130 }
87131}
0 commit comments