API
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Startup.cs 8.2KB

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