예제 #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //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.AddSession();
            services.AddMvc(config =>
            {
                // config.Filters.Add(new AuthenticationFilter());
                config.Filters.Add(new ExtJsResponseBodyFilter());
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .AddJsonOptions(x =>
            {
                x.SerializerSettings.ContractResolver = new DefaultContractResolver
                {
                    NamingStrategy = new CamelCaseNamingStrategy()
                };
                x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                x.SerializerSettings.Formatting            = Formatting.Indented;
            });

            // services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(
            //     options =>
            //     {
            //         options.LoginPath = new PathString("/login");
            //         options.AccessDeniedPath = new PathString("/Denied");
            //     }
            // );

            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(o =>
            {
                o.TokenValidationParameters = new TokenValidationParameters
                {
                    NameClaimType = JwtClaimTypes.Name,
                    RoleClaimType = JwtClaimTypes.Role,

                    ValidIssuer      = GlobalConstants.JWT_ISSURER_URL,
                    ValidAudience    = "api",
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(GlobalConstants.JWT_SECRET_STRING)),

                    ValidateAudience = false,
                    ValidateIssuer   = false,
                };

                o.Events = new JwtBearerEvents()
                {
                    OnMessageReceived = context =>
                    {
                        if (context.Token == null)
                        {
                            SetTokenFromSession(context);
                        }
                        return(Task.CompletedTask);
                    }
                };
            }).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
            {
            });

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            GlobalConstants.DbContextFactory = new DbContextFactory();
            ImmsDbContext.RegisterModelBuilders(new Imms.Security.Data.SecurityModelBuilder());
            ImmsDbContext.RegisterModelBuilders(new Imms.Mes.Data.MesModelBuilder());

            GlobalConstants.GetCurrentUserDelegate = Security.Data.SystemUserLogic.GetCurrentUser;

            services.AddSignalR();
            services.AddHttpClient();

            services.AddSingleton <Sync4WDBService, Sync4WDBService>();
            // services.AddSingleton<Command2TerminatorService, Command2TerminatorService>();
            // services.AddSingleton<CloseSessionService, CloseSessionService>();

            LoginWithBackgroundService();
        }