示例#1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(Configuration);

            string baseUrl         = _hostingEnvironment.IsDevelopment() ? Path.Combine(_hostingEnvironment.ContentRootPath, "bin") : _hostingEnvironment.WebRootPath;
            var    localTextWriter = new LocalTextWriter(baseUrl);

            services.AddSingleton <ITextWriter>(localTextWriter);

            services.RegisterDbContext(Configuration.GetConnectionString("DefaultConnection"));

            services.RegisterRepositories();
            services.RegisterServices();
            services.RegisterSecretVault(Configuration["Security:Vault:Provider"]);

            services.AddAppIdentity();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(opt =>
            {
                opt.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidIssuer              = Configuration["Security:Tokens:Issuer"],
                    ValidateAudience         = true,
                    ValidAudience            = Configuration["Security:Tokens:Audience"],
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Security:Tokens:Key"])),
                    RequireExpirationTime    = false
                };
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddCorrelationId();

            services.AddAutoMapper();

            services.AddAuthorization(options =>
            {
                options.AddPolicy(AuthorizePolicy.ProjectAccess, policy => policy.Requirements.Add(new ProjectAccessRequirement()));
                options.AddPolicy(AuthorizePolicy.ProjectOwnerAccess, policy => policy.Requirements.Add(new ProjectAccessRequirement(MemberRole.Owner)));
                options.AddPolicy(AuthorizePolicy.ProjectMaintainerAccess, policy => policy.Requirements.Add(new ProjectAccessRequirement(MemberRole.Maintainer)));
                options.AddPolicy(AuthorizePolicy.ProjectContributorAccess, policy => policy.Requirements.Add(new ProjectAccessRequirement(MemberRole.Contributor)));
                options.AddPolicy(AuthorizePolicy.ProjectMemberAccess, policy => policy.Requirements.Add(new ProjectAccessRequirement(MemberRole.Member)));
                options.AddPolicy(AuthorizePolicy.UserRoleAdminAccess, policy => policy.RequireRole(UserRole.Administrator));
                options.AddPolicy(AuthorizePolicy.UserRoleBasicAccess, policy => policy.RequireRole(UserRole.Administrator, UserRole.Basic));
                options.AddPolicy(AuthorizePolicy.UserRoleGuestAccess, policy => policy.RequireRole(UserRole.Administrator, UserRole.Basic, UserRole.Guest));
                options.AddPolicy(AuthorizePolicy.UserRoleEngineAccess, policy => policy.RequireRole(UserRole.Administrator, UserRole.Engine));
                options.AddPolicy(AuthorizePolicy.UserRoleBasicEngineAccess, policy => policy.RequireRole(UserRole.Administrator, UserRole.Basic, UserRole.Engine));
            });

            services.AddSingleton <IAuthorizationHandler, ProjectAccessHandler>();
            services.AddSingleton <IAuthorizationHandler, ProjectEngineAccessHandler>();

            services.AddSignalR();

            services.AddNotifications(Configuration);

            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info {
                    Title = "OpenCatapult API", Version = "v1"
                });
                c.CustomSchemaIds(x => x.FullName);

                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });

            services.AddCors();
        }
示例#2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(Configuration);

            string baseUrl = _hostingEnvironment.IsDevelopment() ? Path.Combine(_hostingEnvironment.ContentRootPath, "bin") : _hostingEnvironment.WebRootPath;

            services.Configure <ApplicationSettingValue>(Configuration);
            services.AddScoped(sp => sp.GetService <IOptionsSnapshot <ApplicationSettingValue> >().Value);

            var localTextWriter = new LocalTextWriter(baseUrl);

            services.AddSingleton <ITextWriter>(localTextWriter);

            services.RegisterDbContext(Configuration.GetConnectionString("DefaultConnection"), Configuration["DatabaseProvider"]);

            services.RegisterRepositories();
            services.RegisterServices();
            services.RegisterSecretVault(Configuration["Security:Vault:Provider"]);

            services.AddAppIdentity(Configuration["DatabaseProvider"]);

            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); // => remove default claims
            services
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(opt =>
            {
                opt.RequireHttpsMetadata      = false;
                opt.SaveToken                 = true;
                opt.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidIssuer              = Configuration["Security:Tokens:Issuer"],
                    ValidateAudience         = true,
                    ValidAudience            = Configuration["Security:Tokens:Audience"],
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Security:Tokens:Key"])),
                    RequireExpirationTime    = false
                };
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddCorrelationId();

            services.AddAutoMapper();

            services.AddAuthorization(options =>
            {
                options.AddPolicy(AuthorizePolicy.ProjectAccess, policy => policy.Requirements.Add(new ProjectAccessRequirement()));
                options.AddPolicy(AuthorizePolicy.ProjectOwnerAccess, policy => policy.Requirements.Add(new ProjectAccessRequirement(MemberRole.Owner)));
                options.AddPolicy(AuthorizePolicy.ProjectMaintainerAccess, policy => policy.Requirements.Add(new ProjectAccessRequirement(MemberRole.Maintainer)));
                options.AddPolicy(AuthorizePolicy.ProjectContributorAccess, policy => policy.Requirements.Add(new ProjectAccessRequirement(MemberRole.Contributor)));
                options.AddPolicy(AuthorizePolicy.ProjectMemberAccess, policy => policy.Requirements.Add(new ProjectAccessRequirement(MemberRole.Member)));
                options.AddPolicy(AuthorizePolicy.UserRoleAdminAccess, policy => policy.RequireRole(UserRole.Administrator));
                options.AddPolicy(AuthorizePolicy.UserRoleBasicAccess, policy => policy.RequireRole(UserRole.Administrator, UserRole.Basic));
                options.AddPolicy(AuthorizePolicy.UserRoleGuestAccess, policy => policy.RequireRole(UserRole.Administrator, UserRole.Basic, UserRole.Guest));
                options.AddPolicy(AuthorizePolicy.UserRoleEngineAccess, policy => policy.RequireRole(UserRole.Administrator, UserRole.Engine));
                options.AddPolicy(AuthorizePolicy.UserRoleBasicEngineAccess, policy => policy.RequireRole(UserRole.Administrator, UserRole.Basic, UserRole.Engine));
            });

            services.AddSingleton <IAuthorizationHandler, ProjectAccessHandler>();
            services.AddSingleton <IAuthorizationHandler, ProjectEngineAccessHandler>();

            services.AddSignalR();

            services.AddNotifications(Configuration);

            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info {
                    Title = "OpenCatapult API", Version = "v1"
                });
                c.CustomSchemaIds(x => x.FullName);
                c.AddSecurityDefinition("Bearer", new ApiKeyScheme {
                    In = "header", Description = "Please enter JWT with Bearer into field", Name = "Authorization", Type = "apiKey"
                });
                c.OperationFilter <SecurityRequirementsOperationFilter>();

                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });

            services.AddCors(options =>
            {
                options.AddPolicy(_allowSpecificOriginsPolicy, builder => builder
                                  .WithOrigins(Configuration["AllowedOrigin"].Split(","))
                                  .AllowAnyHeader()
                                  .AllowAnyMethod()
                                  .AllowCredentials()
                                  );
            });
        }