· 7 years ago · Mar 31, 2018, 09:54 PM
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Net;
5using System.Threading.Tasks;
6using Microsoft.AspNetCore.Authentication.Cookies;
7using Microsoft.AspNetCore.Builder;
8using Microsoft.AspNetCore.Hosting;
9using Microsoft.AspNetCore.Identity;
10using Microsoft.EntityFrameworkCore;
11using Microsoft.Extensions.Configuration;
12using Microsoft.Extensions.DependencyInjection;
13using Microsoft.IdentityModel.Tokens;
14using System.Text;
15using Stolikomat.Models;
16using Microsoft.AspNetCore.Authentication.JwtBearer;
17using Microsoft.AspNetCore.Mvc;
18using Microsoft.AspNetCore.Mvc.Cors.Internal;
19
20namespace Stolikomat
21{
22 public class Startup
23 {
24 public Startup(IConfiguration configuration)
25 {
26 Configuration = configuration;
27 }
28
29 public IConfiguration Configuration { get; }
30
31 // This method gets called by the runtime. Use this method to add services to the container.
32 public void ConfigureServices(IServiceCollection services)
33 {
34 var connection = @"Data Source=den1.mssql6.gear.host;Persist Security Info=True;User ID=stolikomatdb2;Password=StolikomatZPI!";
35
36 /*services.Configure<MvcOptions>(options =>
37 {
38 options.Filters.Add(new RequireHttpsAttribute());
39 });*/
40
41 services.AddEntityFrameworkSqlServer()
42 .AddDbContext<UserContext>(options => options.UseSqlServer(connection));
43
44
45 services.AddIdentity<UserItem, IdentityRole>()
46 .AddEntityFrameworkStores<UserContext>();
47
48 services.Configure<JWTSettings>(Configuration.GetSection("JWTSettings"));
49 services.AddDbContext<RestaurantContext>(options => options.UseSqlServer(connection));
50
51 services.ConfigureApplicationCookie(options => options.Events = new CookieAuthenticationEvents
52 {
53 OnRedirectToLogin = ctx =>
54 {
55 ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
56 return Task.FromResult(0);
57 }
58 });
59 var secretKey = Configuration.GetSection("JWTSettings:SecretKey").Value;
60 var issuer = Configuration.GetSection("JWTSettings:Issuer").Value;
61 var audience = Configuration.GetSection("JWTSettings:Audience").Value;
62 var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretKey));
63 var tokenValidationParameters = new TokenValidationParameters
64 {
65 ValidateIssuerSigningKey = true,
66 IssuerSigningKey = signingKey,
67
68 // Validate the JWT Issuer (iss) claim
69 ValidateIssuer = true,
70 ValidIssuer = issuer,
71
72 // Validate the JWT Audience (aud) claim
73 ValidateAudience = true,
74 ValidAudience = audience
75 };
76
77 services.AddAuthentication(options =>
78 {
79 options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
80 })
81 .AddJwtBearer(jwtOptions =>
82 {
83 jwtOptions.TokenValidationParameters = tokenValidationParameters;
84 });
85
86 services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme);
87
88 services.AddCors();
89
90 services.AddMvc();
91
92 }
93
94 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
95 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
96 {
97 if (env.IsDevelopment())
98 {
99 app.UseDeveloperExceptionPage();
100 }
101
102 /*var options = new RewriteOptions().AddRedirectToHttps();
103 app.UseRewriter(options);*/
104
105 app.UseExceptionHandler();
106 app.UseAuthentication();
107
108 app.UseCors(builder =>
109 builder
110 .AllowAnyOrigin()
111 .AllowAnyHeader()
112 .AllowAnyMethod());
113
114 app.UseMvc();
115 }
116 }
117}