예제 #1
0
파일: AdminService.cs 프로젝트: ntzw/cms
        public async Task <Administrator> GetByLoginAdmin()
        {
            var adminLogin = AdminCookieAttribute.GetLoginAdmin();

            if (adminLogin == null || adminLogin.AccountName.IsEmpty())
            {
                return(null);
            }

            var cacheKey = adminLogin.Id + adminLogin.AccountName;

            var loginAdmin = CacheHelper.Get <Administrator>(cacheKey);

            if (loginAdmin != null)
            {
                return(loginAdmin);
            }

            Administrator admin = await GetById(adminLogin.Id.ToInt());

            CacheHelper.Set(cacheKey, admin);

            return(admin);
        }
예제 #2
0
파일: Startup.cs 프로젝트: ntzw/cms
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.TryAddSingleton <IActionResultExecutor <ResultBase>, ResultBaseExecutor>();
            services.AddMemoryCache();
            services.AddDistributedMemoryCache();
            services.AddSession();
            services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All));
            services.AddAuthentication().AddCookie(AdminCookieAttribute.Scheme, o =>
            {
                o.ClaimsIssuer        = AdminCookieAttribute.ClaimsIssuer;
                o.LoginPath           = new PathString("/Admin/Login");
                o.Cookie.SameSite     = SameSiteMode.Unspecified;
                o.Cookie.SecurePolicy = CookieSecurePolicy.None;

                o.Events = new CookieAuthenticationEvents
                {
                    OnValidatePrincipal = async context =>
                    {
                        var admin = AdminCookieAttribute.GetLoginAdmin(context.Principal.Claims);
                        if (admin == null || admin.Id.ToInt() <= 0)
                        {
                            await AdminValidateFail(context);
                            return;
                        }

                        AdminCookieAttribute.SetLoginAdmin(admin);
                    }
                };
            });

            services.AddControllersWithViews(options =>
            {
                options.InputFormatters.Insert(0, new JsonInputFormatter());
            });

            services.AddRazorPages().AddRazorRuntimeCompilation();

            var baseType             = typeof(IModuleInitializer);
            var path                 = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
            var referencedAssemblies = System.IO.Directory.GetFiles(path, "*.dll").ToList().FindAll(temp =>
            {
                string fileName = Path.GetFileName(temp);
                return(fileName.StartsWith("CMS.Modules"));
            }).Select(Assembly.LoadFrom).ToArray();
            var types = referencedAssemblies
                        .SelectMany(a => a.DefinedTypes)
                        .Select(type => type.AsType())
                        .Where(x => x != baseType && baseType.IsAssignableFrom(x)).ToArray();
            var implementTypes = types.Where(x => x.IsClass).ToArray();

            foreach (var implementType in implementTypes)
            {
                services.AddSingleton(baseType, implementType);
            }

            var sp = services.BuildServiceProvider();
            var moduleInitializers = sp.GetServices <IModuleInitializer>();

            foreach (var moduleInitializer in moduleInitializers)
            {
                moduleInitializer.ConfigureServices(services);
            }
        }