Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Microsoft.AspNetCore.Authentication.JwtBearer;
  2. using Microsoft.AspNetCore.Builder;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Mvc.Cors.Internal;
  6. using Microsoft.EntityFrameworkCore;
  7. using Microsoft.Extensions.Configuration;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Microsoft.IdentityModel.Tokens;
  10. using Newtonsoft.Json;
  11. using ProRestaurant.Classes;
  12. using ProRestaurant.DBContexts;
  13. using ProRestaurant.Repository.Accounts;
  14. using ProRestaurant.Repository.Restaurants;
  15. using System.Text;
  16. namespace ProRestaurant
  17. {
  18. public class Startup
  19. {
  20. public Startup(IConfiguration configuration)
  21. {
  22. Configuration = configuration;
  23. }
  24. public IConfiguration Configuration { get; }
  25. public void ConfigureServices(IServiceCollection services)
  26. {
  27. services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
  28. {
  29. builder.AllowAnyOrigin()
  30. .AllowAnyMethod()
  31. .AllowAnyHeader();
  32. }));
  33. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  34. services.AddMvc().AddJsonOptions(options => {
  35. options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
  36. });
  37. #region Authentication Setup
  38. // configure strongly typed settings objects
  39. var appSettingsSection = Configuration.GetSection("AppSettings");
  40. services.Configure<AppSettings>(appSettingsSection);
  41. // configure jwt authentication
  42. var appSettings = appSettingsSection.Get<AppSettings>();
  43. var key = Encoding.ASCII.GetBytes(appSettings.Secret);
  44. services.AddAuthentication(x =>
  45. {
  46. x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  47. x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  48. })
  49. .AddJwtBearer(x =>
  50. {
  51. x.RequireHttpsMetadata = false;
  52. x.SaveToken = true;
  53. x.TokenValidationParameters = new TokenValidationParameters
  54. {
  55. ValidateIssuerSigningKey = true,
  56. IssuerSigningKey = new SymmetricSecurityKey(key),
  57. ValidateIssuer = false,
  58. ValidateAudience = false
  59. };
  60. });
  61. // configure DI for application services
  62. services.AddScoped<IAuthenticateRepository, AuthenticateRepository>();
  63. #endregion
  64. services.AddDbContext<DBContext>(o => o.UseSqlServer(Configuration.GetConnectionString("DefaultDatabase")));
  65. services.AddTransient<IRegistrationRepository, RegistrationRepository>();
  66. services.AddTransient<IRestaurantCategoryRepository, RestaurantCategoryRepository>();
  67. services.AddTransient<IRestaurantRepository, RestaurantRepository>();
  68. services.AddTransient<IUserRepository, UserRepository>();
  69. services.AddTransient<IMenuCategoryRepository, MenuCategoryRepository>();
  70. services.AddTransient<IMenuOptionRepository, MenuOptionRepository>();
  71. services.AddTransient<IMenuItemRepository, MenuItemRepository>();
  72. services.AddTransient<IMenuRepository, MenuRepository>();
  73. services.AddTransient<IRestaurantUserRepository, RestaurantUserRepository>();
  74. services.AddTransient<IRestaurantRoleRepository, RestaurantRoleRepository>();
  75. services.Configure<MvcOptions>(options =>
  76. {
  77. options.Filters.Add(new CorsAuthorizationFilterFactory("MyPolicy"));
  78. });
  79. }
  80. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  81. {
  82. if (env.IsDevelopment())
  83. {
  84. app.UseDeveloperExceptionPage();
  85. }
  86. else
  87. {
  88. app.UseHsts();
  89. }
  90. app.UseCors(x => x
  91. .AllowAnyOrigin()
  92. .AllowAnyMethod()
  93. .AllowAnyHeader());
  94. app.UseAuthentication();
  95. app.UseHttpsRedirection();
  96. app.UseMvc();
  97. }
  98. }
  99. }