WebApiCore

    0

    0

    using Core.Utilities.IoC;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Mvc;
    using System;
    using System.Linq;
    using Microsoft.Extensions.Logging;
    using Core.Extensions;
    using Core.DependencyResolvers;
    using Core.Utilities.Security.Encyption;
    using Microsoft.AspNetCore.Authentication.JwtBearer;
    using Microsoft.AspNetCore.HttpsPolicy;
    using System.Collections.Generic;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Microsoft.IdentityModel.Tokens;
    using Core.Utilities.Security.Jwt;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.AspNetCore.Http;
    using System.Threading.Tasks;
    
    
    
    namespace WebAPI
    {
      public class Startup
      {
        public Startup(IConfiguration configuration)
        {
          Configuration = configuration;
        }
    
        public IConfiguration Configuration { get; }
    
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
          services.AddControllers();
          services.AddCors(options =>
          {
            options.AddPolicy("AllowOrigin",
              builder => builder.WithOrigins("http://localhost:3000"));
          });
    
          var tokenOptions = Configuration.GetSection("TokenOptions").Get<TokenOptions>();
    
          services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
              options.TokenValidationParameters=new TokenValidationParameters
              {
                ValidateIssuer = true,
                ValidateAudience = true,
                ValidateLifetime = true,
                ValidIssuer = tokenOptions.Issuer,
                ValidAudience = tokenOptions.Audience,
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey)
              };
            });
    
    
    
          services.AddDependencyResolvers(new ICoreModule[]
          {
            new CoreModule(),
          });
        }
    
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
          if (env.IsDevelopment())
          {
            app.UseDeveloperExceptionPage();
          }
    
          app.ConfigureCustomExceptionMiddleware();
    
          app.UseCors(builder => builder.WithOrigins("http://localhost:3000").AllowAnyHeader());
    
          app.UseHttpsRedirection();
    
          app.UseRouting();
    
          app.UseAuthentication();
    
          app.UseAuthorization();
    
          app.UseEndpoints(endpoints =>
          {
            endpoints.MapControllers();
          });
        }
      }
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.