예제 #1
0
 // This method gets called by the runtime. Use this method to add services to the container.
 public void ConfigureServices(IServiceCollection services)
 {
     services.AddCors(options =>
     {
         options.AddPolicy("CorsAPI",
                           builder => builder
                           .AllowAnyOrigin()
                           .AllowAnyHeader()
                           .AllowAnyMethod()
                           );
     });
     services.AddControllers();
     services.AddDbContext <ApplicationDbContext>(options =>
                                                  options.UseNpgsql(Configuration.GetConnectionString("PostgreSQL")));
     // options.UseSqlServer(Configuration.GetConnectionString("SqlServer")));
     services.AddScoped <IAuthRepository, AuthRepository>();
     services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
     {
         options.TokenValidationParameters = new TokenValidationParameters
         {
             ValidateIssuerSigningKey = true,
             IssuerSigningKey         = new SymmetricSecurityKey(
                 Encoding.ASCII.GetBytes(Configuration.GetSection("AppSettings:Token").Value)),
             ValidateIssuer   = false,
             ValidateAudience = false
         };
     });
     services.AddAuthorization(config =>
     {
         config.AddPolicy(Policies.Operator, Policies.OperatorPolicy());
         config.AddPolicy(Policies.Driver, Policies.DriverPolicy());
         config.AddPolicy(Policies.Client, Policies.ClientPolicy());
     });
 }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            var connection = Configuration.GetConnectionString("DefaultConnection");

            services.AddDbContextPool <ProyectContext>(options => options.UseSqlServer(connection));

            services.AddControllers().AddNewtonsoftJson(options =>
                                                        options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                                                        );

            //add cors
            services.AddCors();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => {
                options.RequireHttpsMetadata      = false;
                options.SaveToken                 = true;
                options.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuer   = true,
                    ValidateAudience = true,
                    // ValidateLifetime = true,
                    // ValidateIssuerSigningKey = true,
                    ValidIssuer      = Configuration["Jwt:Issuer"],
                    ValidAudience    = Configuration["Jwt:Audience"],
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:SecretKey"])),
                    //ClockSkew = TimeSpan.Zero
                };
            });


            services.AddAuthorization(config =>
            {
                config.AddPolicy(Policies.Admin, Policies.AdminPolicy());
                config.AddPolicy(Policies.User, Policies.UserPolicy());
                config.AddPolicy(Policies.Client, Policies.ClientPolicy());
            });
        }