Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Startup.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 ProRestaurant.Classes;
  19. using ProRestaurant.DBContexts;
  20. using ProRestaurant.Repository.Accounts;
  21. using ProRestaurant.Repository.Restaurants;
  22. namespace ProRestaurant
  23. {
  24. public class Startup
  25. {
  26. public Startup(IConfiguration configuration)
  27. {
  28. Configuration = configuration;
  29. }
  30. public IConfiguration Configuration { get; }
  31. public void ConfigureServices(IServiceCollection services)
  32. {
  33. services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
  34. {
  35. builder.AllowAnyOrigin()
  36. .AllowAnyMethod()
  37. .AllowAnyHeader();
  38. }));
  39. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  40. #region Authentication Setup
  41. // configure strongly typed settings objects
  42. var appSettingsSection = Configuration.GetSection("AppSettings");
  43. services.Configure<AppSettings>(appSettingsSection);
  44. // configure jwt authentication
  45. var appSettings = appSettingsSection.Get<AppSettings>();
  46. var key = Encoding.ASCII.GetBytes(appSettings.Secret);
  47. services.AddAuthentication(x =>
  48. {
  49. x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  50. x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  51. })
  52. .AddJwtBearer(x =>
  53. {
  54. x.RequireHttpsMetadata = false;
  55. x.SaveToken = true;
  56. x.TokenValidationParameters = new TokenValidationParameters
  57. {
  58. ValidateIssuerSigningKey = true,
  59. IssuerSigningKey = new SymmetricSecurityKey(key),
  60. ValidateIssuer = false,
  61. ValidateAudience = false
  62. };
  63. });
  64. // configure DI for application services
  65. services.AddScoped<IAuthenticateRepository, AuthenticateRepository>();
  66. #endregion
  67. services.AddDbContext<DBContext>(o => o.UseSqlServer(Configuration.GetConnectionString("DefaultDatabase")));
  68. services.AddTransient<IRegistrationRepository, RegistrationRepository>();
  69. services.AddTransient<IRestaurantCategoryRepository, RestaurantCategoryRepository>();
  70. services.AddTransient<IRestaurantRepository, RestaurantRepository>();
  71. services.AddTransient<IUserRepository, UserRepository>();
  72. services.AddTransient<IMenuCategoryRepository, MenuCategoryRepository>();
  73. services.Configure<MvcOptions>(options =>
  74. {
  75. options.Filters.Add(new CorsAuthorizationFilterFactory("MyPolicy"));
  76. });
  77. }
  78. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  79. {
  80. if (env.IsDevelopment())
  81. {
  82. app.UseDeveloperExceptionPage();
  83. }
  84. else
  85. {
  86. app.UseHsts();
  87. }
  88. app.UseCors(x => x
  89. .AllowAnyOrigin()
  90. .AllowAnyMethod()
  91. .AllowAnyHeader());
  92. app.UseAuthentication();
  93. app.UseHttpsRedirection();
  94. app.UseMvc();
  95. }
  96. }
  97. }