Пример #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add Cors
            services.AddCors(o => o.AddPolicy(MyAllowSpecificOrigins, builder =>
            {
                string[] hosts = { "localhost", "127.0.0.1", "www.sergego.com" };

                string[] protocols = { "http", "https", Configuration["WSProtocol"] };
                var urls           = new List <string>();
                foreach (var host in hosts)
                {
                    foreach (var protocol in protocols)
                    {
                        urls.Add(protocol + "://" + host);
                        urls.Add(protocol + "://" + host + ":" + Configuration["WebPort"]);
                        urls.Add(protocol + "://" + host + ":" + Configuration["MessagingPort"]);
                    }
                }

                urls.Add(Configuration["DebugClientURL"]);
#if DEBUG
                urls.Add("http://localhost:4200");
#endif
                urls.Add(Configuration["ExternalClientURL"]);
                var withoutDuplicates = new HashSet <string>();
                foreach (var url in urls)
                {
                    withoutDuplicates.Add(url);
                }

                var array = new string[withoutDuplicates.Count];
                withoutDuplicates.CopyTo(array);
                builder.WithOrigins(array)

                /*  builder.WithOrigins("http://localhost:" + Configuration["WebPort"],
                 *                   "http://www.sergego.com:" + Configuration["WebPort"],
                 *                   Configuration["DebugClientURL"],
                 *                   Configuration["ExternalClientURL"],
                 *                   "http://localhost:2020",
                 *                   "http://localhost:4200",
                 *                   "wss://localhost:" + Configuration["MessagingPort"],
                 *                   "wss://www.sergego.com:" + Configuration["MessagingPort"],
                 *                   "http://www.sergego.com", "http://www.sergego.com/fincore") */
                // builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials();
            }));

            var jwtSettings = JwtSettings.FromConfiguration(Configuration);

            services.AddSingleton(jwtSettings);

            services.AddHttpContextAccessor();

            services.Configure <CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddAutoMapper(GetType().Assembly);

            services.AddAuthorization(options =>
            {
                options.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
                                  .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
                                  .RequireAuthenticatedUser().Build());
                options.AddPolicy("admin", policy => policy.RequireClaim("can_delete", "true"));
                options.AddPolicy("user", policy => policy.RequireClaim("can_view", "true"));
            });

            services
            .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options => options.TokenValidationParameters = jwtSettings.TokenValidationParameters);

            services.AddControllersWithViews().AddNewtonsoftJson(options =>
            {
                // Return JSON responses in LowerCase?
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
                // Resolve Looping navigation properties
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            });

            var angularFolder = AngularPath(Configuration["AngularDir"]);

            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration => { configuration.RootPath = angularFolder; });

            services.AddHostedService <MessagingBackgroundService>();
            services.AddSingleton <IMessagingService, MessagingService>();
        }
Пример #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add Cors
            services.AddCors(o => o.AddPolicy(MyAllowSpecificOrigins, builder =>
            {
                builder.WithOrigins("http://localhost:" + Configuration["WebPort"],
                                    Configuration["DebugClientURL"],
                                    Configuration["ExternalClientURL"],
                                    "wss://localhost:" + Configuration["MessagingPort"],
                                    "wss://www.sergego.com:" + Configuration["MessagingPort"],
                                    "http://www.sergego.com", "http://www.sergego.com/fincore")
                // builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials();
            }));

            var jwtSettings = JwtSettings.FromConfiguration(Configuration);

            services.AddSingleton(jwtSettings);

            services.AddHttpContextAccessor();

            services.Configure <CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddAutoMapper(GetType().Assembly);

            services.AddAuthorization(options =>
            {
                options.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
                                  .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
                                  .RequireAuthenticatedUser().Build());
                options.AddPolicy("admin", policy => policy.RequireClaim("can_delete", "true"));
                options.AddPolicy("user", policy => policy.RequireClaim("can_view", "true"));
            });

            services
            .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options => options.TokenValidationParameters = jwtSettings.TokenValidationParameters);

            services.AddControllersWithViews().AddNewtonsoftJson((options =>
            {
                // Return JSON responses in LowerCase?
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
                // Resolve Looping navigation properties
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            }));

            string angularFolder = AngularPath(Configuration["AngularDir"]);

            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = angularFolder;
            });

            services.AddHostedService <MessagingBackgroundService>();
            services.AddSingleton <IMessagingService, MessagingService>();
        }