-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathStartup.cs
More file actions
124 lines (107 loc) · 4.93 KB
/
Startup.cs
File metadata and controls
124 lines (107 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace Microsoft.SCIM.WebHostSample
{
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Microsoft.SCIM.WebHostSample.Provider;
using Newtonsoft.Json;
public class Startup
{
private readonly IWebHostEnvironment environment;
private readonly IConfiguration configuration;
public IMonitor MonitoringBehavior { get; set; }
public IProvider ProviderBehavior { get; set; }
public Startup(IWebHostEnvironment env, IConfiguration configuration)
{
this.environment = env;
this.configuration = configuration;
this.MonitoringBehavior = new ConsoleMonitor();
this.ProviderBehavior = new InMemoryProvider();
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
void ConfigureMvcNewtonsoftJsonOptions(MvcNewtonsoftJsonOptions options) => options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
void ConfigureAuthenticationOptions(AuthenticationOptions options)
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}
void ConfigureJwtBearerOptons( JwtBearerOptions options)
{
if (this.environment.IsDevelopment())
{
options.TokenValidationParameters =
new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = false,
ValidateIssuerSigningKey = false,
ValidIssuer = this.configuration["Token:TokenIssuer"],
ValidAudience = this.configuration["Token:TokenAudience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(this.configuration["Token:IssuerSigningKey"]))
};
}
else
{
options.Authority = this.configuration["Token:TokenIssuer"];
options.Audience = this.configuration["Token:TokenAudience"];
options.Events = new JwtBearerEvents
{
OnTokenValidated = context =>
{
return Task.CompletedTask;
},
OnAuthenticationFailed = AuthenticationFailed
};
}
}
services.AddAuthentication(ConfigureAuthenticationOptions).AddJwtBearer(ConfigureJwtBearerOptons);
services.AddControllers().AddNewtonsoftJson(ConfigureMvcNewtonsoftJsonOptions);
services.AddSingleton(typeof(IProvider), this.ProviderBehavior);
services.AddSingleton(typeof(IMonitor), this.MonitoringBehavior);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
if (this.environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHsts();
app.UseRouting();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(
(IEndpointRouteBuilder endpoints) =>
{
endpoints.MapDefaultControllerRoute();
});
}
private Task AuthenticationFailed(AuthenticationFailedContext arg)
{
// For debugging purposes only!
string authenticationExceptionMessage = $"{{AuthenticationFailed: '{arg.Exception.Message}'}}";
arg.Response.ContentLength = authenticationExceptionMessage.Length;
arg.Response.Body.WriteAsync(
Encoding.UTF8.GetBytes(authenticationExceptionMessage),
0,
authenticationExceptionMessage.Length);
return Task.FromException(arg.Exception);
}
}
}