|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.AspNetCore.Authentication.JwtBearer; |
| 7 | +using Microsoft.AspNetCore.Builder; |
| 8 | +using Microsoft.AspNetCore.Hosting; |
| 9 | +using Microsoft.AspNetCore.Http; |
| 10 | +using Microsoft.AspNetCore.Routing; |
| 11 | +using Microsoft.Extensions.DependencyInjection; |
| 12 | +using Microsoft.Extensions.Hosting; |
| 13 | +using Microsoft.IdentityModel.Tokens; |
| 14 | +using Microsoft.SCIM.WebHostSample.Provider; |
| 15 | + |
| 16 | +namespace Microsoft.SCIM.WebHostSample |
| 17 | +{ |
| 18 | + public class Startup |
| 19 | + { |
| 20 | + public IMonitor MonitoringBehavior { get; set; } |
| 21 | + public IProvider ProviderBehavior { get; set; } |
| 22 | + |
| 23 | + public Startup() |
| 24 | + { |
| 25 | + this.MonitoringBehavior = new ConsoleMonitor(); |
| 26 | + this.ProviderBehavior = new InMemoryProvider(); |
| 27 | + } |
| 28 | + |
| 29 | + // This method gets called by the runtime. Use this method to add services to the container. |
| 30 | + // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 |
| 31 | + public void ConfigureServices(IServiceCollection services) |
| 32 | + { |
| 33 | + services.AddAuthentication(options => |
| 34 | + { |
| 35 | + options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; |
| 36 | + options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; |
| 37 | + options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; |
| 38 | + }) |
| 39 | + .AddJwtBearer(options => |
| 40 | + { |
| 41 | + options.TokenValidationParameters = |
| 42 | + new TokenValidationParameters |
| 43 | + { |
| 44 | + ValidateIssuer = false, |
| 45 | + ValidateAudience = false, |
| 46 | + ValidateLifetime = false, |
| 47 | + ValidateIssuerSigningKey = false, |
| 48 | + ValidIssuer = ServiceConstants.TokenIssuer, |
| 49 | + ValidAudience = ServiceConstants.TokenAudience, |
| 50 | + IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(ServiceConstants.TokenIssuer)) |
| 51 | + }; |
| 52 | + }); |
| 53 | + |
| 54 | + services.AddControllers().AddNewtonsoftJson(); |
| 55 | + |
| 56 | + services.AddSingleton(typeof(IProvider), this.ProviderBehavior); |
| 57 | + services.AddSingleton(typeof(IMonitor), this.MonitoringBehavior); |
| 58 | + } |
| 59 | + |
| 60 | + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
| 61 | + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) |
| 62 | + { |
| 63 | + if (env.IsDevelopment()) |
| 64 | + { |
| 65 | + app.UseDeveloperExceptionPage(); |
| 66 | + } |
| 67 | + |
| 68 | + app.UseHsts(); |
| 69 | + |
| 70 | + app.UseRouting(); |
| 71 | + app.UseHttpsRedirection(); |
| 72 | + app.UseAuthentication(); |
| 73 | + app.UseAuthorization(); |
| 74 | + |
| 75 | + app.UseEndpoints( |
| 76 | + (IEndpointRouteBuilder endpoints) => |
| 77 | + { |
| 78 | + endpoints.MapDefaultControllerRoute(); |
| 79 | + }); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments