1
- // Copyright (c) Microsoft Corporation.// Licensed under the MIT license.
2
-
3
- using System ;
4
- using System . Collections . Generic ;
5
- using System . Linq ;
6
- using System . Text ;
7
- using System . Threading . Tasks ;
8
- using Microsoft . AspNetCore . Authentication . JwtBearer ;
9
- using Microsoft . AspNetCore . Builder ;
10
- using Microsoft . AspNetCore . Hosting ;
11
- using Microsoft . AspNetCore . Http ;
12
- using Microsoft . AspNetCore . Routing ;
13
- using Microsoft . Extensions . Configuration ;
14
- using Microsoft . Extensions . DependencyInjection ;
15
- using Microsoft . Extensions . Hosting ;
16
- using Microsoft . IdentityModel . Tokens ;
17
- using Microsoft . SCIM . WebHostSample . Provider ;
1
+ //------------------------------------------------------------
2
+ // Copyright (c) Microsoft Corporation. All rights reserved.
3
+ //------------------------------------------------------------
18
4
19
5
namespace Microsoft . SCIM . WebHostSample
20
6
{
7
+ using System . Text ;
8
+ using System . Threading . Tasks ;
9
+ using Microsoft . AspNetCore . Authentication . JwtBearer ;
10
+ using Microsoft . AspNetCore . Builder ;
11
+ using Microsoft . AspNetCore . Hosting ;
12
+ using Microsoft . AspNetCore . Routing ;
13
+ using Microsoft . Extensions . Configuration ;
14
+ using Microsoft . Extensions . DependencyInjection ;
15
+ using Microsoft . Extensions . Hosting ;
16
+ using Microsoft . IdentityModel . Tokens ;
17
+ using Microsoft . SCIM . WebHostSample . Provider ;
18
+
21
19
public class Startup
22
20
{
23
- private readonly IWebHostEnvironment _env ;
24
- private readonly IConfiguration _configuration ;
21
+ private readonly IWebHostEnvironment environment ;
22
+ private readonly IConfiguration configuration ;
25
23
26
24
public IMonitor MonitoringBehavior { get ; set ; }
27
25
public IProvider ProviderBehavior { get ; set ; }
28
26
29
27
public Startup ( IWebHostEnvironment env , IConfiguration configuration )
30
28
{
31
- this . _env = env ;
32
- this . _configuration = configuration ;
29
+ this . environment = env ;
30
+ this . configuration = configuration ;
33
31
34
32
this . MonitoringBehavior = new ConsoleMonitor ( ) ;
35
33
this . ProviderBehavior = new InMemoryProvider ( ) ;
@@ -39,7 +37,7 @@ public Startup(IWebHostEnvironment env, IConfiguration configuration)
39
37
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
40
38
public void ConfigureServices ( IServiceCollection services )
41
39
{
42
- if ( _env . IsDevelopment ( ) )
40
+ if ( this . environment . IsDevelopment ( ) )
43
41
{
44
42
// Development environment code
45
43
// Validation for bearer token for authorization used during testing.
@@ -51,20 +49,20 @@ public void ConfigureServices(IServiceCollection services)
51
49
options . DefaultAuthenticateScheme = JwtBearerDefaults . AuthenticationScheme ;
52
50
options . DefaultChallengeScheme = JwtBearerDefaults . AuthenticationScheme ;
53
51
} )
54
- . AddJwtBearer ( options =>
55
- {
56
- options . TokenValidationParameters =
57
- new TokenValidationParameters
58
- {
59
- ValidateIssuer = false ,
60
- ValidateAudience = false ,
61
- ValidateLifetime = false ,
62
- ValidateIssuerSigningKey = false ,
63
- ValidIssuer = this . _configuration [ "Token:TokenIssuer" ] ,
64
- ValidAudience = this . _configuration [ "Token:TokenAudience" ] ,
65
- IssuerSigningKey = new SymmetricSecurityKey ( Encoding . UTF8 . GetBytes ( this . _configuration [ "Token:IssuerSigningKey" ] ) )
66
- } ;
67
- } ) ;
52
+ . AddJwtBearer ( options =>
53
+ {
54
+ options . TokenValidationParameters =
55
+ new TokenValidationParameters
56
+ {
57
+ ValidateIssuer = false ,
58
+ ValidateAudience = false ,
59
+ ValidateLifetime = false ,
60
+ ValidateIssuerSigningKey = false ,
61
+ ValidIssuer = this . configuration [ "Token:TokenIssuer" ] ,
62
+ ValidAudience = this . configuration [ "Token:TokenAudience" ] ,
63
+ IssuerSigningKey = new SymmetricSecurityKey ( Encoding . UTF8 . GetBytes ( this . configuration [ "Token:IssuerSigningKey" ] ) )
64
+ } ;
65
+ } ) ;
68
66
}
69
67
else
70
68
{
@@ -79,33 +77,32 @@ public void ConfigureServices(IServiceCollection services)
79
77
options . DefaultAuthenticateScheme = JwtBearerDefaults . AuthenticationScheme ;
80
78
options . DefaultChallengeScheme = JwtBearerDefaults . AuthenticationScheme ;
81
79
} )
82
- . AddJwtBearer ( options =>
80
+ . AddJwtBearer ( options =>
81
+ {
82
+ options . Authority = this . configuration [ "Token:TokenIssuer" ] ;
83
+ options . Audience = this . configuration [ "Token:TokenAudience" ] ;
84
+ options . Events = new JwtBearerEvents
83
85
{
84
- options . Authority = this . _configuration [ "Token:TokenIssuer" ] ;
85
- options . Audience = this . _configuration [ "Token:TokenAudience" ] ;
86
- options . Events = new JwtBearerEvents
86
+ OnTokenValidated = context =>
87
87
{
88
- OnTokenValidated = context =>
89
- {
90
- // NOTE: You can optionally take action when the OAuth 2.0 bearer token was validated.
88
+ // NOTE: You can optionally take action when the OAuth 2.0 bearer token was validated.
91
89
92
- return Task . CompletedTask ;
93
- } ,
94
- OnAuthenticationFailed = AuthenticationFailed
95
- } ;
96
- } ) ;
90
+ return Task . CompletedTask ;
91
+ } ,
92
+ OnAuthenticationFailed = AuthenticationFailed
93
+ } ;
94
+ } ) ;
97
95
}
98
96
99
97
services . AddControllers ( ) . AddNewtonsoftJson ( ) ;
100
-
101
98
services . AddSingleton ( typeof ( IProvider ) , this . ProviderBehavior ) ;
102
99
services . AddSingleton ( typeof ( IMonitor ) , this . MonitoringBehavior ) ;
103
100
}
104
101
105
102
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
106
103
public void Configure ( IApplicationBuilder app )
107
104
{
108
- if ( _env . IsDevelopment ( ) )
105
+ if ( this . environment . IsDevelopment ( ) )
109
106
{
110
107
app . UseDeveloperExceptionPage ( ) ;
111
108
}
@@ -127,10 +124,13 @@ public void Configure(IApplicationBuilder app)
127
124
private Task AuthenticationFailed ( AuthenticationFailedContext arg )
128
125
{
129
126
// For debugging purposes only!
130
- var s = $ "{{AuthenticationFailed: '{ arg . Exception . Message } '}}";
127
+ string authenticationExceptionMessage = $ "{{AuthenticationFailed: '{ arg . Exception . Message } '}}";
131
128
132
- arg . Response . ContentLength = s . Length ;
133
- arg . Response . Body . WriteAsync ( Encoding . UTF8 . GetBytes ( s ) , 0 , s . Length ) ;
129
+ arg . Response . ContentLength = authenticationExceptionMessage . Length ;
130
+ arg . Response . Body . WriteAsync (
131
+ Encoding . UTF8 . GetBytes ( authenticationExceptionMessage ) ,
132
+ 0 ,
133
+ authenticationExceptionMessage . Length ) ;
134
134
135
135
return Task . FromException ( arg . Exception ) ;
136
136
}
0 commit comments