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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. .WithOrigins("http://training.provision-sa.com:122/")
  54. .AllowAnyMethod()
  55. .AllowAnyHeader();
  56. }));
  57. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  58. services.AddDbContext<DataContext>(o => o.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
  59. MyCommon.TenderUrl = Configuration.GetConnectionString("TenderConnection");
  60. var appSettingsSection = Configuration.GetSection("AppSettings");
  61. services.Configure<AppSettings>(appSettingsSection);
  62. // configure jwt authentication
  63. var appSettings = appSettingsSection.Get<AppSettings>();
  64. var key = Encoding.ASCII.GetBytes(appSettings.Secret);
  65. services.AddAuthentication(x =>
  66. {
  67. x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  68. x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  69. })
  70. .AddJwtBearer(x =>
  71. {
  72. x.Events = new JwtBearerEvents
  73. {
  74. OnTokenValidated = context =>
  75. {
  76. var registerRepository = context.HttpContext.RequestServices.GetRequiredService<IRegisterRepository>();
  77. var userId = int.Parse(context.Principal.Identity.Name);
  78. var user = registerRepository.GetById(userId);
  79. if (user == null)
  80. {
  81. // return unauthorized if user no longer exists
  82. context.Fail("Unauthorized");
  83. }
  84. return Task.CompletedTask;
  85. }
  86. };
  87. x.RequireHttpsMetadata = false;
  88. x.SaveToken = true;
  89. x.TokenValidationParameters = new TokenValidationParameters
  90. {
  91. ValidateIssuerSigningKey = true,
  92. IssuerSigningKey = new SymmetricSecurityKey(key),
  93. ValidateIssuer = false,
  94. ValidateAudience = false
  95. };
  96. });
  97. services.Configure<IISServerOptions>(options =>
  98. {
  99. options.AutomaticAuthentication = true;
  100. });
  101. #region
  102. services.AddTransient<IBankRepository, BankRepository>();
  103. #endregion
  104. #region ProcessFlow
  105. services.AddTransient<IBidRepository, BidRepository>();
  106. #endregion
  107. #region Property
  108. services.AddTransient<IRepository<Agent>, AgentRepository>();
  109. services.AddTransient<IRegisterRepository, RegisterRepository>();
  110. services.AddTransient<IRepository<Agency>, AgencyRepository>();
  111. services.AddTransient<IRepository<Email>, EmailRepository>();
  112. services.AddTransient<IRepository<SMTPAccount>, SMTPAccountRepository>();
  113. services.AddTransient<IRepository<SMTPHost>, SMTPHostRepository>();
  114. services.AddTransient<IPropertyRepository, PropertyRepository>();
  115. services.AddTransient<IPropertyImageRepository, PropertyImageRepository>();
  116. services.AddTransient<IRepository<PropertyType>, PropertyTypeRepository>();
  117. services.AddTransient<IRepository<PropertyUserField>, PropertyUserFieldRepository>();
  118. services.AddTransient<IRepository<UserDefinedField>, UserDefinedFieldRepository>();
  119. services.AddTransient<IUserDefinedGroupRepository, UserDefinedGroupRepository>();
  120. services.AddTransient<IRepository<Payment>, PaymentRepository>();
  121. #endregion Property
  122. #region Region
  123. services.AddTransient<IRepository<Province>, ProvinceRepository>();
  124. #endregion Region
  125. #region Timeshare
  126. services.AddTransient<IRepository<Status>, StatusRepository>();
  127. services.AddTransient<IRepository<Season>, SeasonRepository>();
  128. services.AddTransient<IRepository<UnitConfiguration>, UnitConfigurationRepository>();
  129. services.AddTransient<IRepository<TimeshareWeek>, WeekRepository>();
  130. services.AddTransient<IWeekRepository, WeekRepository>();
  131. services.AddTransient<IRepository<Bank>, BankAccountRepository>();
  132. services.AddTransient<IRepository<BankAccount>, BankAccountRepository>();
  133. services.AddTransient<IResortRepository, ResortRepository>();
  134. #endregion Timeshare
  135. #region User
  136. services.AddScoped<IRegisterRepository, RegisterRepository>();
  137. services.AddTransient<IRepository<Agent>, AgentRepository>();
  138. services.AddTransient<IRegisterRepository, RegisterRepository>();
  139. services.AddTransient<IRepository<Agency>, AgencyRepository>();
  140. services.AddTransient<IRepository<User>, UserRepository>();
  141. services.AddTransient<IRepository<Individual>, IndividualRepository>();
  142. #endregion User
  143. #region Communication
  144. services.AddTransient<IRepository<Template>, TemplateRepository>();
  145. services.AddTransient<IRepository<Email>, EmailRepository>();
  146. services.AddTransient<IRepository<SMTPAccount>, SMTPAccountRepository>();
  147. services.AddTransient<IRepository<SMTPHost>, SMTPHostRepository>();
  148. services.AddTransient<IMailRepository, MailRepository>();
  149. #endregion Communication
  150. #region Logs
  151. services.AddTransient<ISearchLogRepository, SearchLogRepository>();
  152. #endregion
  153. #region Financial
  154. services.AddTransient<IPaygateRepository, PaygateRepository>();
  155. services.AddTransient<IListingRepository, ListingRepository>();
  156. #endregion
  157. #region Misc
  158. services.AddTransient<ICarouselRepository, CarouselRepository>();
  159. services.AddTransient<IRepository<PlaceHolderFormat>, PlaceHolderFormatRepository>();
  160. #endregion
  161. #region Campaign
  162. services.AddTransient<ICampaignRepository, CampaignRepository>();
  163. #endregion
  164. services.Configure<MvcOptions>(options =>
  165. {
  166. options.Filters.Add(new CorsAuthorizationFilterFactory("MyPolicy"));
  167. });
  168. }
  169. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  170. {
  171. UpdateDatabase(app);
  172. if (env.IsDevelopment())
  173. {
  174. app.UseDeveloperExceptionPage();
  175. }
  176. app.UseCors(x => x
  177. .WithOrigins("http://training.provision-sa.com:122/")
  178. .AllowAnyOrigin()
  179. .AllowAnyMethod()
  180. .AllowAnyHeader());
  181. app.UseAuthentication();
  182. app.UseHttpsRedirection();
  183. app.UseMvc();
  184. }
  185. private static void UpdateDatabase(IApplicationBuilder app)
  186. {
  187. using (var serviceScope = app.ApplicationServices
  188. .GetRequiredService<IServiceScopeFactory>()
  189. .CreateScope())
  190. {
  191. using (var context = serviceScope.ServiceProvider.GetService<DataContext>())
  192. {
  193. //context.Database.Migrate();
  194. }
  195. }
  196. }
  197. }
  198. }