API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Startup.cs 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using AutoMapper;
  2. using Microsoft.AspNetCore.Authentication.JwtBearer;
  3. using Microsoft.AspNetCore.Builder;
  4. using Microsoft.AspNetCore.Hosting;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Mvc.Cors.Internal;
  7. using Microsoft.EntityFrameworkCore;
  8. using Microsoft.Extensions.Configuration;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using Microsoft.IdentityModel.Tokens;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using UnivateProperties_API.Context;
  14. using UnivateProperties_API.Helpers;
  15. using UnivateProperties_API.Model.Banks;
  16. using UnivateProperties_API.Model.Communication;
  17. using UnivateProperties_API.Model.ProcessFlow;
  18. using UnivateProperties_API.Model.Properties;
  19. using UnivateProperties_API.Model.Region;
  20. using UnivateProperties_API.Model.Timeshare;
  21. using UnivateProperties_API.Model.Users;
  22. using UnivateProperties_API.Repository;
  23. using UnivateProperties_API.Repository.Banks;
  24. using UnivateProperties_API.Repository.Communication;
  25. using UnivateProperties_API.Repository.Logging;
  26. using UnivateProperties_API.Repository.ProccessFlow;
  27. using UnivateProperties_API.Repository.Properties;
  28. using UnivateProperties_API.Repository.Region;
  29. using UnivateProperties_API.Repository.Timeshare;
  30. using UnivateProperties_API.Repository.Users;
  31. namespace UnivateProperties_API
  32. {
  33. public class Startup
  34. {
  35. public Startup(IConfiguration configuration)
  36. {
  37. Configuration = configuration;
  38. }
  39. public IConfiguration Configuration { get; }
  40. public void ConfigureServices(IServiceCollection services)
  41. {
  42. services.AddAutoMapper();
  43. services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
  44. {
  45. builder.AllowAnyOrigin()
  46. .AllowAnyMethod()
  47. .AllowAnyHeader();
  48. }));
  49. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  50. services.AddDbContext<DataContext>(o => o.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
  51. MyCommon.TenderUrl = Configuration.GetConnectionString("TenderConnection");
  52. var appSettingsSection = Configuration.GetSection("AppSettings");
  53. services.Configure<AppSettings>(appSettingsSection);
  54. // configure jwt authentication
  55. var appSettings = appSettingsSection.Get<AppSettings>();
  56. var key = Encoding.ASCII.GetBytes(appSettings.Secret);
  57. services.AddAuthentication(x =>
  58. {
  59. x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  60. x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  61. })
  62. .AddJwtBearer(x =>
  63. {
  64. x.Events = new JwtBearerEvents
  65. {
  66. OnTokenValidated = context =>
  67. {
  68. var registerRepository = context.HttpContext.RequestServices.GetRequiredService<IRegisterRepository>();
  69. var userId = int.Parse(context.Principal.Identity.Name);
  70. var user = registerRepository.GetById(userId);
  71. if (user == null)
  72. {
  73. // return unauthorized if user no longer exists
  74. context.Fail("Unauthorized");
  75. }
  76. return Task.CompletedTask;
  77. }
  78. };
  79. x.RequireHttpsMetadata = false;
  80. x.SaveToken = true;
  81. x.TokenValidationParameters = new TokenValidationParameters
  82. {
  83. ValidateIssuerSigningKey = true,
  84. IssuerSigningKey = new SymmetricSecurityKey(key),
  85. ValidateIssuer = false,
  86. ValidateAudience = false
  87. };
  88. });
  89. #region ProcessFlow
  90. services.AddTransient<IBidRepository, BidRepository>();
  91. #endregion
  92. #region Property
  93. services.AddTransient<IRepository<Agent>, AgentRepository>();
  94. services.AddTransient<IRegisterRepository, RegisterRepository>();
  95. services.AddTransient<IRepository<Agency>, AgencyRepository>();
  96. services.AddTransient<IRepository<Email>, EmailRepository>();
  97. services.AddTransient<IRepository<SMTPAccount>, SMTPAccountRepository>();
  98. services.AddTransient<IRepository<SMTPHost>, SMTPHostRepository>();
  99. services.AddTransient<IPropertyRepository, PropertyRepository>();
  100. services.AddTransient<IPropertyImageRepository, PropertyImageRepository>();
  101. services.AddTransient<IRepository<PropertyType>, PropertyTypeRepository>();
  102. services.AddTransient<IRepository<PropertyUserField>, PropertyUserFieldRepository>();
  103. services.AddTransient<IRepository<UserDefinedField>, UserDefinedFieldRepository>();
  104. services.AddTransient<IUserDefinedGroupRepository, UserDefinedGroupRepository>();
  105. #endregion Property
  106. #region Region
  107. services.AddTransient<IRepository<Province>, ProvinceRepository>();
  108. services.AddTransient<ICityRepository, CityRepository>();
  109. services.AddTransient<ISuburbRepository, SuburbRepository>();
  110. #endregion Region
  111. #region Timeshare
  112. services.AddTransient<IRepository<Status>, StatusRepository>();
  113. services.AddTransient<IRepository<Season>, SeasonRepository>();
  114. services.AddTransient<IRepository<UnitConfiguration>, UnitConfigurationRepository>();
  115. services.AddTransient<IRepository<TimeshareWeek>, WeekRepository>();
  116. services.AddTransient<IRepository<TimeshareWeek>, WeekRepository>();
  117. services.AddTransient<IRepository<Bank>, BankAccountRepository>();
  118. services.AddTransient<IRepository<BankAccount>, BankAccountRepository>();
  119. #endregion Timeshare
  120. #region User
  121. services.AddScoped<IRegisterRepository, RegisterRepository>();
  122. services.AddTransient<IRepository<Agent>, AgentRepository>();
  123. services.AddTransient<IRegisterRepository, RegisterRepository>();
  124. services.AddTransient<IRepository<Agency>, AgencyRepository>();
  125. services.AddTransient<IRepository<User>, UserRepository>();
  126. services.AddTransient<IRepository<Individual>, IndividualRepository>();
  127. #endregion User
  128. #region Communication
  129. services.AddTransient<IRepository<Template>, TemplateRepository>();
  130. services.AddTransient<IRepository<Email>, EmailRepository>();
  131. services.AddTransient<IRepository<SMTPAccount>, SMTPAccountRepository>();
  132. services.AddTransient<IRepository<SMTPHost>, SMTPHostRepository>();
  133. #endregion Communication
  134. #region Logs
  135. services.AddTransient<ISearchLogRepository, SearchLogRepository>();
  136. #endregion
  137. services.Configure<MvcOptions>(options =>
  138. {
  139. options.Filters.Add(new CorsAuthorizationFilterFactory("MyPolicy"));
  140. });
  141. }
  142. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  143. {
  144. UpdateDatabase(app);
  145. if (env.IsDevelopment())
  146. {
  147. app.UseDeveloperExceptionPage();
  148. }
  149. app.UseCors(x => x
  150. .AllowAnyOrigin()
  151. .AllowAnyMethod()
  152. .AllowAnyHeader());
  153. app.UseAuthentication();
  154. app.UseHttpsRedirection();
  155. app.UseMvc();
  156. }
  157. private static void UpdateDatabase(IApplicationBuilder app)
  158. {
  159. using (var serviceScope = app.ApplicationServices
  160. .GetRequiredService<IServiceScopeFactory>()
  161. .CreateScope())
  162. {
  163. using (var context = serviceScope.ServiceProvider.GetService<DataContext>())
  164. {
  165. //context.Database.Migrate();
  166. }
  167. }
  168. }
  169. }
  170. }