using AutoMapper; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors.Infrastructure; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Cors.Internal; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Tokens; using System.Text; using System.Threading.Tasks; using UnivateProperties_API.Context; using UnivateProperties_API.Helpers; using UnivateProperties_API.Model.Banks; using UnivateProperties_API.Model.Campaigns; using UnivateProperties_API.Model.Communication; using UnivateProperties_API.Model.Financial; using UnivateProperties_API.Model.Misc; using UnivateProperties_API.Model.ProcessFlow; using UnivateProperties_API.Model.Properties; using UnivateProperties_API.Model.Region; using UnivateProperties_API.Model.Timeshare; using UnivateProperties_API.Model.Users; using UnivateProperties_API.Repository; using UnivateProperties_API.Repository.Banks; using UnivateProperties_API.Repository.Campaigns; using UnivateProperties_API.Repository.Communication; using UnivateProperties_API.Repository.Financial; using UnivateProperties_API.Repository.Logging; using UnivateProperties_API.Repository.Misc; using UnivateProperties_API.Repository.ProccessFlow; using UnivateProperties_API.Repository.Properties; using UnivateProperties_API.Repository.Region; using UnivateProperties_API.Repository.Timeshare; using UnivateProperties_API.Repository.Users; namespace UnivateProperties_API { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddAutoMapper(); services.AddCors(o => o.AddPolicy("MyPolicy", builder => { builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); })); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddDbContext(o => o.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); MyCommon.TenderUrl = Configuration.GetConnectionString("TenderConnection"); MyCommon.Reservations = Configuration.GetConnectionString("ReservationsURL"); MyCommon.ReservationsUserCode = Configuration.GetConnectionString("ReservationsUserCode"); MyCommon.ReservationsPassword = Configuration.GetConnectionString("ReservationsPassword"); var appSettingsSection = Configuration.GetSection("AppSettings"); services.Configure(appSettingsSection); // configure jwt authentication var appSettings = appSettingsSection.Get(); 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 registerRepository = context.HttpContext.RequestServices.GetRequiredService(); var userId = int.Parse(context.Principal.Identity.Name); var user = registerRepository.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.Configure(options => { options.AutomaticAuthentication = true; }); #region services.AddTransient(); #endregion #region ProcessFlow services.AddTransient(); #endregion #region Property services.AddTransient, AgentRepository>(); services.AddTransient(); services.AddTransient, AgencyRepository>(); services.AddTransient, EmailRepository>(); services.AddTransient, SMTPAccountRepository>(); services.AddTransient, SMTPHostRepository>(); services.AddTransient(); services.AddTransient(); services.AddTransient, PropertyTypeRepository>(); services.AddTransient, PropertyUserFieldRepository>(); services.AddTransient, UserDefinedFieldRepository>(); services.AddTransient(); services.AddTransient, PaymentRepository>(); #endregion Property #region Region services.AddTransient, ProvinceRepository>(); #endregion Region #region Timeshare services.AddTransient, StatusRepository>(); services.AddTransient, SeasonRepository>(); services.AddTransient, UnitConfigurationRepository>(); services.AddTransient, WeekRepository>(); services.AddTransient(); services.AddTransient, BankAccountRepository>(); services.AddTransient, BankAccountRepository>(); services.AddTransient(); #endregion Timeshare #region User services.AddScoped(); services.AddTransient, AgentRepository>(); services.AddTransient(); services.AddTransient, AgencyRepository>(); services.AddTransient, UserRepository>(); services.AddTransient, IndividualRepository>(); #endregion User #region Communication services.AddTransient, TemplateRepository>(); services.AddTransient, EmailRepository>(); services.AddTransient, SMTPAccountRepository>(); services.AddTransient, SMTPHostRepository>(); services.AddTransient(); #endregion Communication #region Logs services.AddTransient(); #endregion #region Financial services.AddTransient(); services.AddTransient(); #endregion #region Misc services.AddTransient(); services.AddTransient, PlaceHolderFormatRepository>(); services.AddTransient(); #endregion #region Campaign services.AddTransient(); #endregion services.Configure(options => { options.Filters.Add(new CorsAuthorizationFilterFactory("MyPolicy")); }); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { UpdateDatabase(app); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseCors(x => x .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader()); app.UseAuthentication(); app.UseHttpsRedirection(); app.UseMvc(); } private static void UpdateDatabase(IApplicationBuilder app) { using (var serviceScope = app.ApplicationServices .GetRequiredService() .CreateScope()) { using (var context = serviceScope.ServiceProvider.GetService()) { //context.Database.Migrate(); } } } } }