@@ -18,13 +18,15 @@ namespace Microsoft.SCIM.WebHostSample
18
18
{
19
19
public class Startup
20
20
{
21
+ private readonly IWebHostEnvironment _env ;
21
22
private readonly IConfiguration _configuration ;
22
23
23
24
public IMonitor MonitoringBehavior { get ; set ; }
24
25
public IProvider ProviderBehavior { get ; set ; }
25
26
26
- public Startup ( IConfiguration configuration )
27
+ public Startup ( IWebHostEnvironment env , IConfiguration configuration )
27
28
{
29
+ this . _env = env ;
28
30
this . _configuration = configuration ;
29
31
30
32
this . MonitoringBehavior = new ConsoleMonitor ( ) ;
@@ -35,26 +37,57 @@ public Startup(IConfiguration configuration)
35
37
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
36
38
public void ConfigureServices ( IServiceCollection services )
37
39
{
38
- services . AddAuthentication ( options =>
40
+ if ( _env . IsDevelopment ( ) )
39
41
{
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 =>
45
46
{
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
48
80
{
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
56
88
} ;
57
- } ) ;
89
+ } ) ;
90
+ }
58
91
59
92
services . AddControllers ( ) . AddNewtonsoftJson ( ) ;
60
93
@@ -63,9 +96,9 @@ public void ConfigureServices(IServiceCollection services)
63
96
}
64
97
65
98
// 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 )
67
100
{
68
- if ( env . IsDevelopment ( ) )
101
+ if ( _env . IsDevelopment ( ) )
69
102
{
70
103
app . UseDeveloperExceptionPage ( ) ;
71
104
}
@@ -83,5 +116,16 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
83
116
endpoints . MapDefaultControllerRoute ( ) ;
84
117
} ) ;
85
118
}
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
+ }
86
130
}
87
131
}
0 commit comments