123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Authentication.JwtBearer;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.HttpsPolicy;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.Cors.Internal;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using Microsoft.IdentityModel.Tokens;
- using ProRestaurant.Classes;
- using ProRestaurant.DBContexts;
- using ProRestaurant.Repository.Accounts;
- using ProRestaurant.Repository.Restaurants;
-
- namespace ProRestaurant
- {
- public class Startup
- {
- public Startup(IConfiguration configuration)
- {
- Configuration = configuration;
- }
-
- public IConfiguration Configuration { get; }
-
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
- {
- builder.AllowAnyOrigin()
- .AllowAnyMethod()
- .AllowAnyHeader();
- }));
- services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
-
- #region Authentication Setup
- // configure strongly typed settings objects
- var appSettingsSection = Configuration.GetSection("AppSettings");
- services.Configure<AppSettings>(appSettingsSection);
-
- // configure jwt authentication
- var appSettings = appSettingsSection.Get<AppSettings>();
- var key = Encoding.ASCII.GetBytes(appSettings.Secret);
- services.AddAuthentication(x =>
- {
- x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
- x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
- })
- .AddJwtBearer(x =>
- {
- x.RequireHttpsMetadata = false;
- x.SaveToken = true;
- x.TokenValidationParameters = new TokenValidationParameters
- {
- ValidateIssuerSigningKey = true,
- IssuerSigningKey = new SymmetricSecurityKey(key),
- ValidateIssuer = false,
- ValidateAudience = false
- };
- });
-
- // configure DI for application services
- services.AddScoped<IAuthenticateRepository, AuthenticateRepository>();
- #endregion
-
-
- services.AddDbContext<DBContext>(o => o.UseSqlServer(Configuration.GetConnectionString("DefaultDatabase")));
-
- services.AddTransient<IRegistrationRepository, RegistrationRepository>();
- services.AddTransient<IRestaurantCategoryRepository, RestaurantCategoryRepository>();
- services.AddTransient<IRestaurantRepository, RestaurantRepository>();
-
- services.Configure<MvcOptions>(options =>
- {
- options.Filters.Add(new CorsAuthorizationFilterFactory("MyPolicy"));
- });
- }
-
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- else
- {
- app.UseHsts();
- }
-
- app.UseHttpsRedirection();
- app.UseMvc();
- }
- }
- }
|