Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <AvivatecDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));


            services.AddScoped <IUsuarioRepository, UsuarioRepository>();


            RepositoryScopeInjector.Add(services);
            ServiceScopeInjector.Add(services);


            services.AddCors(o => o.AddPolicy("Policy", builder =>
            {
                builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader();
            }));



            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1",
                             new Info
                {
                    Title   = "Avivatec API",
                    Version = "v1",
                }
                             );
            });



            services.AddMvcCore()//.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddAuthorization()
            .AddJsonFormatters()
            .AddApiExplorer();


            services.AddLogging();
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <RodolfoSchmidtDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));

            RepositoryScopeInjector.Add(services);
            ServiceScopeInjector.Add(services);

            services.AddCors(o => o.AddPolicy("Cors", builder =>
            {
                builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader();
            }));

            services.AddAutoMapper(typeof(MappingProfile));
            services.AddMvcCore().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .AddAuthorization()
            .AddJsonFormatters()
            .AddApiExplorer();
        }
Exemplo n.º 3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <ReclotiContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));

            services.AddAutoMapper(typeof(MappinProfiles));

            RepositoryScopeInjector.Add(services);
            ServiceScopeInjector.Add(services);

            services.AddSingleton <ILogger>(LogManager.GetCurrentClassLogger());

            services.AddCors(options =>
            {
                options.AddPolicy("AllowMyOrigin",
                                  builder => builder.AllowAnyOrigin()
                                  .AllowAnyHeader()
                                  .AllowAnyMethod());
            });

            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); // => remove default claims
            services
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(cfg =>
            {
                cfg.RequireHttpsMetadata      = false;
                cfg.SaveToken                 = true;
                cfg.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidIssuer      = Configuration["JwtIssuer"],
                    ValidAudience    = Configuration["JwtIssuer"],
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtKey"])),
                    ClockSkew        = TimeSpan.Zero // remove delay of token when expire
                };
                cfg.Events = new JwtBearerEvents
                {
                    OnTokenValidated = context =>
                    {
                        // Add the access_token as a claim, as we may actually need it
                        var accessToken = context.SecurityToken as JwtSecurityToken;
                        if (accessToken != null)
                        {
                            ClaimsIdentity identity = context.Principal.Identity as ClaimsIdentity;
                            if (identity != null)
                            {
                                identity.AddClaim(new Claim(ClaimTypes.Role, accessToken.Claims.Where(c => c.Type == ClaimTypes.Role).First().Value));
                                identity.AddClaim(new Claim("id", accessToken.Claims.Where(c => c.Type == "id").First().Value));
                                identity.AddClaim(new Claim("name", accessToken.Claims.Where(c => c.Type == "name").First().Value));
                                identity.AddClaim(new Claim("email", accessToken.Claims.Where(c => c.Type == "email").First().Value));
                            }
                        }

                        return(Task.CompletedTask);
                    }
                };
            });

            services.AddMvcCore().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddAuthorization()
            .AddNewtonsoftJson()
            .AddApiExplorer();

            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
            {
                options.LoginPath = "/Home";
            });

            services.AddControllers();
        }