API
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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