· 7 years ago · Jan 07, 2019, 09:38 PM
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using api.Infrastructure;
7using Microsoft.AspNetCore.Authentication.JwtBearer;
8using Microsoft.AspNetCore.Builder;
9using Microsoft.AspNetCore.Hosting;
10using Microsoft.AspNetCore.HttpsPolicy;
11using Microsoft.AspNetCore.Mvc;
12using Microsoft.Extensions.Configuration;
13using Microsoft.Extensions.DependencyInjection;
14using Microsoft.Extensions.Logging;
15using Microsoft.Extensions.Options;
16using Microsoft.IdentityModel.Tokens;
17using Microsoft.EntityFrameworkCore;
18using model;
19using Swashbuckle.AspNetCore.Swagger;
20
21namespace api
22{
23 public class Startup
24 {
25 public Startup(IConfiguration configuration)
26 {
27 Configuration = configuration;
28 }
29
30 public IConfiguration Configuration { get; }
31
32 // This method gets called by the runtime. Use this method to add services to the container.
33 public void ConfigureServices(IServiceCollection services)
34 {
35 services.AddDbContext<SmartCityContext>(config => config.UseSqlServer(Configuration.GetConnectionString("smartCityCS")));
36 string SecretKey = "MaSuperCleSecret";//TODO in app setting
37 SymmetricSecurityKey _signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(SecretKey));
38 services.Configure<JwtIssuerOptions>(options =>
39 {
40 options.Issuer = "ServeurDeJetons";//TODO in app setting
41 options.Audience = "http://localhost:5000";
42 options.SigningCredentials = new SigningCredentials(_signingKey, SecurityAlgorithms.HmacSha256);
43 });
44
45 var tokenValidationParameters = new TokenValidationParameters
46 {
47 ValidateIssuer = true,
48 ValidIssuer = "ServeurDeJetons", //TODO in app setting
49
50 ValidateAudience = true,
51 ValidAudience = "http://localhost:5000",
52
53 ValidateIssuerSigningKey = true,
54 IssuerSigningKey = _signingKey,
55
56 RequireExpirationTime = true,
57 ValidateLifetime = true,
58
59 ClockSkew = TimeSpan.Zero
60 };
61 services
62 .AddAuthentication(
63 options=>
64 {
65 options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
66 })
67 .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options=>
68 {
69 options.Audience = "http://localhost:5000";
70 options.ClaimsIssuer = "ServeurDeJeton"; //TODO in app setting
71 options.TokenValidationParameters = tokenValidationParameters;
72 options.SaveToken = true;
73 });
74 services.AddCors();
75 services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
76 services.AddSwaggerGen(c =>
77 {
78 c.SwaggerDoc("v1", new Info { Title = "NamurFD-API", Version = "v1" });
79 });
80 }
81
82 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
83 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
84 {
85 app.UseSwagger();
86 app.UseSwaggerUI(c =>
87 {
88 c.SwaggerEndpoint("/swagger/v1/swagger.json", "NamurFD v1");
89 });
90 //if (env.IsDevelopment())
91 //{
92 app.UseDeveloperExceptionPage();
93 //}
94 //else
95 //{
96 // app.UseHsts();
97 //}
98
99 //A changer avant la mise en production
100 //app.UseHttpsRedirection();
101 app.UseMvc();
102 //WithOrigins("http://localhost:4200")
103 app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
104 }
105 }
106}