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

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