123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- 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<DataContext>(o => o.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
- MyCommon.TenderUrl = Configuration.GetConnectionString("TenderConnection");
-
- var appSettingsSection = Configuration.GetSection("AppSettings");
- services.Configure<AppSettings>(appSettingsSection);
-
- // configure jwt authentication
- var appSettings = appSettingsSection.Get<AppSettings>();
- 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<IRegisterRepository>();
- 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<IISServerOptions>(options =>
- {
- options.AutomaticAuthentication = true;
- });
-
- #region
- services.AddTransient<IBankRepository, BankRepository>();
- #endregion
- #region ProcessFlow
- services.AddTransient<IBidRepository, BidRepository>();
- #endregion
- #region Property
- services.AddTransient<IRepository<Agent>, AgentRepository>();
- services.AddTransient<IRegisterRepository, RegisterRepository>();
- services.AddTransient<IRepository<Agency>, AgencyRepository>();
- services.AddTransient<IRepository<Email>, EmailRepository>();
- services.AddTransient<IRepository<SMTPAccount>, SMTPAccountRepository>();
- services.AddTransient<IRepository<SMTPHost>, SMTPHostRepository>();
- services.AddTransient<IPropertyRepository, PropertyRepository>();
- services.AddTransient<IPropertyImageRepository, PropertyImageRepository>();
- services.AddTransient<IRepository<PropertyType>, PropertyTypeRepository>();
- services.AddTransient<IRepository<PropertyUserField>, PropertyUserFieldRepository>();
- services.AddTransient<IRepository<UserDefinedField>, UserDefinedFieldRepository>();
- services.AddTransient<IUserDefinedGroupRepository, UserDefinedGroupRepository>();
- services.AddTransient<IRepository<Payment>, PaymentRepository>();
- #endregion Property
- #region Region
- services.AddTransient<IRepository<Province>, ProvinceRepository>();
- #endregion Region
- #region Timeshare
- services.AddTransient<IRepository<Status>, StatusRepository>();
- services.AddTransient<IRepository<Season>, SeasonRepository>();
- services.AddTransient<IRepository<UnitConfiguration>, UnitConfigurationRepository>();
- services.AddTransient<IRepository<TimeshareWeek>, WeekRepository>();
- services.AddTransient<IWeekRepository, WeekRepository>();
- services.AddTransient<IRepository<Bank>, BankAccountRepository>();
- services.AddTransient<IRepository<BankAccount>, BankAccountRepository>();
- services.AddTransient<IResortRepository, ResortRepository>();
- #endregion Timeshare
- #region User
- services.AddScoped<IRegisterRepository, RegisterRepository>();
- services.AddTransient<IRepository<Agent>, AgentRepository>();
- services.AddTransient<IRegisterRepository, RegisterRepository>();
- services.AddTransient<IRepository<Agency>, AgencyRepository>();
- services.AddTransient<IRepository<User>, UserRepository>();
- services.AddTransient<IRepository<Individual>, IndividualRepository>();
- #endregion User
- #region Communication
- services.AddTransient<IRepository<Template>, TemplateRepository>();
- services.AddTransient<IRepository<Email>, EmailRepository>();
- services.AddTransient<IRepository<SMTPAccount>, SMTPAccountRepository>();
- services.AddTransient<IRepository<SMTPHost>, SMTPHostRepository>();
- services.AddTransient<IMailRepository, MailRepository>();
- #endregion Communication
- #region Logs
- services.AddTransient<ISearchLogRepository, SearchLogRepository>();
- #endregion
- #region Financial
- services.AddTransient<IPaygateRepository, PaygateRepository>();
- services.AddTransient<IListingRepository, ListingRepository>();
- #endregion
- #region Misc
- services.AddTransient<ICarouselRepository, CarouselRepository>();
- services.AddTransient<IRepository<PlaceHolderFormat>, PlaceHolderFormatRepository>();
- #endregion
- #region Campaign
- services.AddTransient<ICampaignRepository, CampaignRepository>();
- #endregion
- services.Configure<MvcOptions>(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<IServiceScopeFactory>()
- .CreateScope())
- {
- using (var context = serviceScope.ServiceProvider.GetService<DataContext>())
- {
- //context.Database.Migrate();
- }
- }
- }
- }
- }
|