예제 #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();
            services.AddSignalR();

            services.Configure <RazorViewEngineOptions>(o => {
                o.ViewLocationExpanders.Add(new MyViewLocationExpander());
            });

            services.AddMvc()
            .AddJsonOptions(options => {
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                options.SerializerSettings.Converters.Add(new StringEnumConverter {
                    CamelCaseText = true
                });
            });

            var connections = Configuration.GetSection("ConnectionStrings");

            services.AddDbContext <DataContext>(options => options.UseSqlServer(connections["DefaultConnection"]));

            // IoC
            ServicesModule.AddServices(services);

            var serviceProvider = services.BuildServiceProvider();

            services.AddAuthentication(options => {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(options => {
                Configuration.GetSection("Auth0").Bind(options);
                options.Events = GetJwtBearerEvents(serviceProvider);
            });

            Console.WriteLine("Env: " + HostingEnvironment.EnvironmentName);
            if (!HostingEnvironment.IsDevelopment() && !HostingEnvironment.IsEnvironment("local"))
            {
                // services.Configure<MvcOptions>(options => {
                //     options.Filters.Add(new RequireHttpsAttribute());
                // });
            }
        }
예제 #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddFile(Configuration.GetSection("Logging"));

            if (!HostingEnvironment.IsEnvironment("test"))
            {
                app.UseJwtBearerAuthentication(Jwt.GetJwtOptions());
            }

            app.UseCors(p =>
            {
                p.WithOrigins("");
            });
            //todo:实际项目中,放在登录授权里面
            app.Map("/auth/test", appbuilder =>
            {
                appbuilder.Run(d =>
                {
                    var token = Jwt.SignToken(new List <Claim>()
                    {
                        new Claim("name", "ryan")
                    });

                    return(d.Response.WriteAsync(token));
                });
            });

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    "default",
                    "api/{controller=Values}/{action=Hello}/{id?}");
            });
            if (HostingEnvironment.IsDevelopment())
            {
                loggerFactory.AddDebug();
                app.UseSwagger();
                app.UseSwaggerUi(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs"));
            }
            // appLifetime?.ApplicationStopped.Register(() => this.Container.Dispose());
        }
예제 #3
0
        /// <summary>
        /// Configures the middleware services.
        /// </summary>
        /// <param name="services">The services.</param>
        private static void ConfigureMiddlewareServices(IServiceCollection services)
        {
            services.AddMvc().AddJsonOptions(options =>
            {
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                options.SerializerSettings.TypeNameHandling      = TypeNameHandling.Auto;
                options.SerializerSettings.Formatting            = Formatting.Indented;
                options.SerializerSettings.DateTimeZoneHandling  = DateTimeZoneHandling.Utc;
                options.SerializerSettings.ContractResolver      = new CamelCasePropertyNamesContractResolver();
            }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            if (HostingEnvironment.IsEnvironment("IntegrationTest"))
            {
                services.AddDbContext <SubscriptionServiceConfigurationContext>(builder =>
                                                                                builder.UseInMemoryDatabase("SubscriptionServiceConfigurationContext"))
                .AddTransient <SubscriptionServiceConfigurationContext>();
            }
            else
            {
                var connectionString =
                    Startup.Configuration.GetConnectionString(nameof(SubscriptionServiceConfigurationContext));

                services.AddDbContext <SubscriptionServiceConfigurationContext>(builder =>
                                                                                builder.UseMySql(connectionString))
                .AddTransient <SubscriptionServiceConfigurationContext>();;
            }

            services.AddMvcCore();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info {
                    Title = "Subscription Service Configuration API", Version = "v1"
                });
            });
        }
예제 #4
0
 /// <summary>
 /// Returns true if any of given environment flags matches the current environment setting in
 /// '<seealso cref="IHostingEnvironment"/>.EnvironmentName' (usually set by passing in an argument to the web application,
 /// or by setting the 'ASPNETCORE_ENVIRONMENT' environment variable).
 /// <para>This method requires that the environment names match the names of the enum type names.</para>
 /// </summary>
 /// <param name="environment">Environment flags to test against. If any match, 'true' is returned.</param>
 /// <param name="ignoreCase">If true (default) case is ignored when comparing against the 'IHostingEnvironment.EnvironmentName' value.</param>
 public bool IsEnvironment(Environments environment, bool ignoreCase = true)
 {
     return(HostingEnvironment.IsEnvironment(environment, ignoreCase));
 }
예제 #5
0
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Returns true if the given environment name matches the current environment setting in
        /// '<seealso cref="IHostingEnvironment"/>.EnvironmentName' (usually set by passing in an argument to the web application,
        /// or by setting the 'ASPNETCORE_ENVIRONMENT' environment variable).
        /// </summary>
        public bool IsEnvironment(string environmentName, bool ignoreCase = false)
        {
            return(HostingEnvironment.IsEnvironment(environmentName, ignoreCase));
        }