Skip to content

Commit c3a2108

Browse files
committed
splitted config between development and production
1 parent 1cb3536 commit c3a2108

File tree

3 files changed

+78
-21
lines changed

3 files changed

+78
-21
lines changed

Microsoft.SCIM.WebHostSample/Controllers/TokenController.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ namespace Microsoft.SCIM.WebHostSample.Controllers
1717
public class TokenController : ControllerBase
1818
{
1919
private readonly IConfiguration _configuration;
20-
//private const int TokenLifetimeInMins = 120;
20+
21+
private const int defaultTokenExpiration = 120;
2122

2223
public TokenController(IConfiguration Configuration)
2324
{
@@ -26,14 +27,22 @@ public TokenController(IConfiguration Configuration)
2627

2728
private string GenerateJSONWebToken()
2829
{
30+
// Create token key
2931
SymmetricSecurityKey securityKey =
3032
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(this._configuration["Token:IssuerSigningKey"]));
3133
SigningCredentials credentials =
3234
new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
3335

36+
// Set token expiration
3437
DateTime startTime = DateTime.UtcNow;
35-
DateTime expiryTime = startTime.AddMinutes(double.Parse(this._configuration["Token:TokenLifetimeInMins"]));
38+
DateTime expiryTime;
39+
double tokenExpiration;
40+
if (double.TryParse(this._configuration["Token:TokenLifetimeInMins"], out tokenExpiration))
41+
expiryTime = startTime.AddMinutes(tokenExpiration);
42+
else
43+
expiryTime = startTime.AddMinutes(defaultTokenExpiration);
3644

45+
// Generate the token
3746
JwtSecurityToken token =
3847
new JwtSecurityToken(
3948
this._configuration["Token:TokenIssuer"],

Microsoft.SCIM.WebHostSample/Startup.cs

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Microsoft.SCIM.WebHostSample/appsettings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66
"Microsoft.Hosting.Lifetime": "Information"
77
}
88
},
9+
"Token": {
10+
"TokenAudience": "8adf8e6e-67b2-4cf2-a259-e3dc5476c621",
11+
"TokenIssuer": "https://sts.windows.net/<tenant_id>/"
12+
},
913
"AllowedHosts": "*"
1014
}

0 commit comments

Comments
 (0)