Advertisement
Guest User

Untitled

a guest
Apr 6th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Autofac;
  7. using Autofac.Extensions.DependencyInjection;
  8. using Microsoft.AspNetCore.Authentication.JwtBearer;
  9. using Microsoft.AspNetCore.Builder;
  10. using Microsoft.AspNetCore.Hosting;
  11. using Microsoft.Extensions.Configuration;
  12. using Microsoft.Extensions.DependencyInjection;
  13. using Microsoft.Extensions.Logging;
  14. using Microsoft.Extensions.Options;
  15. using Microsoft.IdentityModel.Tokens;
  16. using NTPProject.Core.Repositories;
  17. using NTPProject.Infrastructure.IoC;
  18. using NTPProject.Infrastructure.IoC.Modules;
  19. using NTPProject.Infrastructure.Mappers;
  20. using NTPProject.Infrastructure.Repositories;
  21. using NTPProject.Infrastructure.Services;
  22. using NTPProject.Infrastructure.Settings;
  23.  
  24. namespace NTPProject.Api
  25. {
  26.     public class Startup
  27.     {
  28.         public IConfiguration Configuration { get; }
  29.  
  30.         public IContainer ApplicationContainer { get; private set; }
  31.  
  32.  
  33.         public Startup(IConfiguration configuration)
  34.         {
  35.             Configuration = configuration;
  36.         }
  37.  
  38.         // This method gets called by the runtime. Use this method to add services to the container.
  39.         public IServiceProvider ConfigureServices(IServiceCollection services)
  40.         {
  41.             services.AddAuthorization(x => x.AddPolicy("admin", p => p.RequireRole("admin")));
  42.  
  43.             services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
  44.                 .AddJwtBearer(options =>
  45.                 {
  46.                     //options.Audience = Configuration.GetSection("jwt:issuer").Value;
  47.                     //options.ClaimsIssuer = Configuration.GetSection("jwt:issuer").Value;
  48.                     options.RequireHttpsMetadata = false;
  49.                     options.SaveToken = true;
  50.  
  51.                     options.TokenValidationParameters = new TokenValidationParameters
  52.                     {
  53.                         ValidIssuer = Configuration.GetSection("jwt:issuer").Value,
  54.                         ValidateAudience = false,
  55.                         IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration.GetSection("jwt:key").Value))
  56.                     };
  57.                 });
  58.  
  59.             services.AddMvc();
  60.  
  61.  
  62.             var builder = new ContainerBuilder();
  63.             builder.Populate(services);
  64.             builder.RegisterModule(new ContainerModule(Configuration));
  65.             ApplicationContainer = builder.Build();
  66.  
  67.             return new AutofacServiceProvider(ApplicationContainer);
  68.         }
  69.  
  70.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  71.         public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime)
  72.         {
  73.             if (env.IsDevelopment())
  74.             {
  75.                 app.UseDeveloperExceptionPage();
  76.             }
  77.  
  78.             app.UseAuthentication();
  79.  
  80.             app.UseMvc();
  81.             appLifetime.ApplicationStopped.Register(() => ApplicationContainer.Dispose());
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement