Ejemplo 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.AddControllers().AddNewtonsoftJson();

            services.Configure <FormOptions>(options =>
            {
                options.MultipartBodyLengthLimit = 100 * 1024 * 1024;
            });
            services.AddMvc(o =>
            {
                var policy = new AuthorizationPolicyBuilder()
                             .RequireAuthenticatedUser()
                             .AddRequirements(new AuthorizationPageRequirement())
                             .Build();
                o.Filters.Add(new AuthorizeFilter(policy));
                o.Filters.Add(typeof(MyActionFilter));
            }).SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
            .AddSessionStateTempDataProvider()    //Prevent request too long
            .AddNewtonsoftJson(options => { options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; });


            services.ConfigureApplicationCookie(options =>
            {
                options.LoginPath        = new PathString("/Account/Login");
                options.AccessDeniedPath = new PathString("/Account/AccessDenied");
                //options.LogoutPath = new PathString("/[your-path]");
            });

            var host = Configuration["DocumentManagementSettings:Host"];
            var stringServerContextLogin = Configuration["DocumentManagementSettings:ServerContextLogin"];

            var serverContextLogin = stringServerContextLogin.Split(";");

            if (serverContextLogin.Length < 2)
            {
                throw new Exception("Destination Server must be set first");
            }

            //set global variable param model
            var globalVariable = new GlobalVariableParamModel()
            {
                ApplicationDomain         = Configuration["GlobalVariable:ApplicationDomain"],
                ApplicationName           = Configuration["GlobalVariable:ApplicationName"],
                ConnectionString          = Configuration["ConnectionStrings:DefaultConnection"],
                ContentRootPath           = environment.ContentRootPath,
                ReportBaseUrl             = Configuration["GlobalVariable:ReportDomain"],
                EmailNotificationUsername = Configuration["GlobalVariable:EmailNotificationUsername"],
                EmailNotificationPassword = Configuration["GlobalVariable:EmailNotificationPassword"],
                EmailNotificationHost     = Configuration["GlobalVariable:EmailNotificationHost"],
                EmailNotificationPort     = Convert.ToInt16(Configuration["GlobalVariable:EmailNotificationPort"]),
                SensenetBaseUrl           = Configuration["GlobalVariable:SensenetBaseUrl"],
                EnvironmentVariable       = !string.IsNullOrEmpty(Configuration["GlobalVariable:EnvironmentVariable"]) ? Configuration["GlobalVariable:EnvironmentVariable"] : "Development"
            };

            services.AddSingleton(globalVariable);

            //services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(AppSettingsJson.GetConnectionString()));
            services.AddDbContext <ApplicationDbContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"]));
            //services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:PetroseaConnection"]));

            //Get from APPSETTING.JSON connectionString
            //add identity
            services.AddIdentity <ApplicationUser, ApplicationRole>(options =>
            {
                options.Password.RequiredLength         = 8;
                options.Password.RequireLowercase       = false;
                options.Password.RequireUppercase       = false;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireDigit           = false;
            })
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            services.AddDistributedMemoryCache();

            services.AddSession(options =>
            {
                // Set a short timeout for easy testing.
                options.IdleTimeout        = TimeSpan.FromSeconds(10);
                options.Cookie.HttpOnly    = true;
                options.Cookie.IsEssential = true;
            });
            //services.AddKendo();

            services.Configure <CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddTransient <IAuthorizationHandler, AuthorizationPageHandler>();

            services.AddScoped <IPrincipal>(sp => sp.GetService <IHttpContextAccessor>().HttpContext.User);
        }
 public MyActionFilter(ApplicationDbContext context, IPrincipal principal, GlobalVariableParamModel globalParameter) : base(context, principal, globalParameter)
 {
 }
 protected BaseRepository(ApplicationDbContext context, IPrincipal principal, GlobalVariableParamModel globalVariable)
 {
     this.context         = context;
     this.principal       = principal;
     this._globalVariable = globalVariable;
 }