Advertisement
Guest User

Startup Config

a guest
May 22nd, 2021
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.91 KB | None | 0 0
  1. using System.Text;
  2. using System.Threading.Tasks;
  3. using Microsoft.AspNetCore.Authentication.JwtBearer;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.Extensions.Configuration;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Microsoft.Extensions.Hosting;
  10. using Microsoft.IdentityModel.Tokens;
  11. using Microsoft.OpenApi.Models;
  12. using Simple_RPG_API.BAL;
  13.  
  14. namespace Simple_RPG_API
  15. {
  16.     public class Startup
  17.     {
  18.  
  19.         public Startup(IConfiguration configuration)
  20.         {
  21.             Configuration = configuration;
  22.         }
  23.  
  24.         public IConfiguration Configuration { get; }
  25.  
  26.         // This method gets called by the runtime. Use this method to add services to the container.
  27.         public void ConfigureServices(IServiceCollection services)
  28.         {
  29.             services.AddScoped<IUserService, UserService>();
  30.             services.AddControllers();
  31.             services.AddCors(options =>
  32.             {
  33.                 options.AddDefaultPolicy(
  34.                     builder =>
  35.                     {
  36.                         builder.WithOrigins("http://localhost:4200")
  37.                                             .AllowAnyHeader()
  38.                                             .AllowAnyMethod();
  39.                     });
  40.             });
  41.             services.AddSwaggerGen(c =>
  42.             {
  43.                 c.SwaggerDoc("v1", new OpenApiInfo { Title = "Simple_RPG_API", Version = "v1" });
  44.             });
  45.  
  46.             services.AddScoped<IUserService, UserService>();
  47.  
  48.             var key = Encoding.ASCII.GetBytes(Configuration["AppSettings:Secret"]);
  49.             services.AddAuthentication(x =>
  50.             {
  51.                 x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  52.                 x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  53.             })
  54.             .AddJwtBearer(x =>
  55.             {
  56.                 x.Events = new JwtBearerEvents
  57.                 {
  58.                     OnTokenValidated = context =>
  59.                     {
  60.                         var userService = context.HttpContext.RequestServices.GetRequiredService<IUserService>();
  61.                         var userId = int.Parse(context.Principal.Identity.Name);
  62.                         var user = userService.GetById(userId);
  63.                         if (user == null)
  64.                         {
  65.                             // return unauthorized if user no longer exists
  66.                             context.Fail("Unauthorized");
  67.                         }
  68.                         return Task.CompletedTask;
  69.                     }
  70.                 };
  71.                 x.RequireHttpsMetadata = false;
  72.                 x.SaveToken = true;
  73.                 x.TokenValidationParameters = new TokenValidationParameters
  74.                 {
  75.                     ValidateIssuerSigningKey = true,
  76.                     IssuerSigningKey = new SymmetricSecurityKey(key),
  77.                     ValidateIssuer = false,
  78.                     ValidateAudience = false
  79.                 };
  80.             });
  81.         }
  82.  
  83.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  84.         public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  85.         {
  86.             if (env.IsDevelopment())
  87.             {
  88.                 app.UseDeveloperExceptionPage();
  89.                 app.UseSwagger();
  90.                 app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Simple_RPG_API v1"));
  91.             }
  92.             app.UseHttpsRedirection();
  93.             app.UseRouting();
  94.             app.UseCors(x => x
  95.                 .AllowAnyOrigin()
  96.                 .AllowAnyMethod()
  97.                 .AllowAnyHeader());
  98.             app.UseAuthorization();
  99.             app.UseEndpoints(endpoints =>
  100.             {
  101.                 endpoints.MapControllers();
  102.             });
  103.         }
  104.     }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement