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 9.2KB

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