| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Authentication.JwtBearer;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.HttpsPolicy;
- // using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using Microsoft.IdentityModel.Tokens;
- using WK_KLI_LAPS_COMMONTOOL_Service.Controllers.Master.User;
- using WK_KLI_LAPS_COMMONTOOL_Service.Hub;
- using WK_KLI_LAPS_COMMONTOOL_Service.Models;
- namespace WK_KLI_LAPS_COMMONTOOL_Service
- {
- public class Startup
- {
- public Startup(IHostingEnvironment env)
- {
- var builder = new ConfigurationBuilder()
- .SetBasePath(env.ContentRootPath)
- .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
- Configuration = builder.Build();
- //Configuration = configuration;
- }
- static public IConfigurationRoot Configuration { get; set; }
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddCors();
- services.AddControllers();
- //services.AddCors(options =>
- //{
- // options.AddPolicy("CorsPolicy",
- // builder => builder.AllowAnyOrigin()
- // .AllowAnyMethod()
- // .AllowAnyHeader()
- // .AllowCredentials());
- //});
- services.AddCors(options =>
- {
- options.AddPolicy("CorsPolicy", builder =>
- builder.SetIsOriginAllowed(_ => true)
- .AllowAnyMethod()
- .AllowAnyHeader()
- .AllowCredentials());
- });
- services.AddSignalR();
- services.AddMvc();
- services.AddSingleton<IConfiguration>(Configuration);
- // configure strongly typed settings objects
- var appSettingsSection = Configuration.GetSection("AppSettings");
- services.Configure<Appsettings>(appSettingsSection);
- // configure jwt authentication
- var appSettings = appSettingsSection.Get<Appsettings>();
- //hangfire
- //services.AddHangfire(config =>
- // config.UsePostgreSqlStorage(appSettings.ConnectionStringSchedule));
- var key = Encoding.ASCII.GetBytes(appSettings.Secret);
- services.AddAuthentication(x =>
- {
- x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
- x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
- })
- .AddJwtBearer(x =>
- {
- x.Events = new JwtBearerEvents
- {
- OnTokenValidated = context =>
- {
- var userService = context.HttpContext.RequestServices.GetRequiredService<IUserService>();
- var userId = int.Parse(context.Principal.Identity.Name);
- var user = userService.GetById(userId);
- if (user == null)
- {
- // return unauthorized if user no longer exists
- context.Fail("Unauthorized");
- }
- return Task.CompletedTask;
- }
- };
- x.RequireHttpsMetadata = false;
- x.SaveToken = true;
- x.TokenValidationParameters = new TokenValidationParameters
- {
- ValidateIssuerSigningKey = true,
- IssuerSigningKey = new SymmetricSecurityKey(key),
- ValidateIssuer = false,
- ValidateAudience = false
- };
- });
- services.AddHttpContextAccessor();
- //// configure DI for application services
- services.AddScoped<IUserService, UserService>();
-
- // services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
-
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
- {
- //if (env.IsDevelopment())
- //{
- // app.UseDeveloperExceptionPage();
- //}
- //else
- //{
- // // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
- // app.UseHsts();
- //}
- //app.UseHttpsRedirection();
- //app.UseMvc();
- app.Use(async (ctx, next) =>
- {
- await next();
- if (ctx.Response.StatusCode == 204)
- {
- ctx.Response.ContentLength = 0;
- }
- });
- // app.UseCors("CorsPolicy");
- app.UseSignalR(routes =>
- {
- routes.MapHub<AuthHub>("/authhub");
- });
- //if (env.IsDevelopment())
- //{
- // app.UseDeveloperExceptionPage();
- //}
- //else
- //{
- // app.UseHsts();
- //}
- WK_KLI_LAPS_COMMONTOOL_Service.App_Data.AppContext.Configure(app.ApplicationServices.GetRequiredService<IHttpContextAccessor>());
- app.UseAuthentication();
- app.UseHttpsRedirection();
- // app.UseMvc();
- app.UseRouting();
- app.UseCors("CorsPolicy");
- app.UseAuthorization();
- app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
- }
- }
- }
|