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

            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseSqlite(
                                                             Configuration.GetConnectionString("DefaultConnection")));

            //services.AddAuthorization(options =>
            //{
            //    options.AddPolicy("admin", policy => policy.RequireRole("admin"));
            //}); // Policy方式权限验证

            services.AddAuthentication("BasicAuthentication")
            .AddScheme <AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicAuthentication", null);

            if (!_env.IsDevelopment())
            {
                services.AddHttpsRedirection(options =>
                {
                    options.RedirectStatusCode = StatusCodes.Status308PermanentRedirect;
                    options.HttpsPort          = 443;
                });
            }
            else
            {
                services.AddHttpsRedirection(options =>
                {
                    options.RedirectStatusCode = StatusCodes.Status307TemporaryRedirect;
                    options.HttpsPort          = 27000;
                });
            }

            services.AddScoped <IUserService, UserService>();
            services.AddSingleton(service =>
            {
                var fileExplorer = new FileExplorer();

                string homePath = (Environment.OSVersion.Platform == PlatformID.Unix ||
                                   Environment.OSVersion.Platform == PlatformID.MacOSX)
                    ? Environment.GetEnvironmentVariable("HOME")
                    : Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
                fileExplorer.InitializeHomeDirectory(new Uri(/*@"E:\milkitic\source\GitHub\BSTServer-Rework"*/ homePath));
                return(fileExplorer);
            });

            services.Configure <ApiBehaviorOptions>(options =>
            {
                options.InvalidModelStateResponseFactory = actionContext =>
                {
                    return(new BadRequestObjectResult(new
                    {
                        code = 400.1,
                        message = "字段验证错误",
                        data = actionContext.ModelState.ToDictionary(k => k.Key,
                                                                     k => string.Join(";", k.Value.Errors.Select(o => o.ErrorMessage)))
                    }));
                };
            });

            services.Configure <FormOptions>(x =>
            {
                x.ValueLengthLimit            = int.MaxValue;
                x.MultipartBodyLengthLimit    = long.MaxValue;
                x.MultipartHeadersLengthLimit = int.MaxValue;
            });
        }