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

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