コード例 #1
0
        public static IMvcBuilder ConfigureMvc(this IServiceCollection services, IConfiguration configuration)
        {
            IMvcBuilder builder = services.AddMvc();

            builder.AddRazorPagesOptions(options =>
            {
                options.Conventions.AuthorizeFolder("/");

                options.Conventions.AllowAnonymousToPage("/Error");
                options.Conventions.AllowAnonymousToPage("/Account/AccessDenied");
                options.Conventions.AllowAnonymousToPage("/Account/ConfirmEmail");
                options.Conventions.AllowAnonymousToPage("/Account/ExternalLogin");
                options.Conventions.AllowAnonymousToPage("/Account/ForgotPassword");
                options.Conventions.AllowAnonymousToPage("/Account/ForgotPasswordConfirmation");
                options.Conventions.AllowAnonymousToPage("/Account/Lockout");
                options.Conventions.AllowAnonymousToPage("/Account/Login");
                options.Conventions.AllowAnonymousToPage("/Account/LoginWith2fa");
                options.Conventions.AllowAnonymousToPage("/Account/LoginWithRecoveryCode");
                options.Conventions.AllowAnonymousToPage("/Account/Register");
                options.Conventions.AllowAnonymousToPage("/Account/ResetPassword");
                options.Conventions.AllowAnonymousToPage("/Account/ResetPasswordConfirmation");
                options.Conventions.AllowAnonymousToPage("/Account/SignedOut");
            });

            builder.SetCompatibilityVersion(CompatibilityVersion.Latest);

            return(builder);
        }
コード例 #2
0
 /// <summary>
 /// 添加Razor页面约定
 /// </summary>
 /// <param name="builder">Mvc生成器</param>
 public static void AddRazorPageConventions(this IMvcBuilder builder)
 {
     builder.AddRazorPagesOptions(options => {
         options.Conventions.Add(new ViewPageRouteConvention());
         options.Conventions.AddFolderApplicationModelConvention("/", model => model.Filters.Add(new GeneratePageFilter()));
     });
 }
コード例 #3
0
        public IMvcBuilder AuthorizeConfig(IMvcBuilder builder, IHostEnvironment env)
        {
            builder.Services
            .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(Utils.ConfigurationHelper.SWAGGER_ATUH_COOKIE, options =>
            {
                if (Utils.ConfigurationHelper.CustsomSwaggerOptions.UseAdminAuth)
                {
                    options.LoginPath = "/Admin/Login/";
                }
                else
                {
                    options.LoginPath = $"/{Utils.ConfigurationHelper.CustsomSwaggerOptions.RoutePrefix}/login.html";
                }
                options.Cookie.HttpOnly = false;
            });

            builder.AddRazorPagesOptions(options =>
            {
                //此处可配置页面权限
            });

            SenparcTrace.SendCustomLog("Swagger 启动", "完成 Area:Senparc.Xncf.Swagger 注册");

            return(builder);
        }
コード例 #4
0
        /// <summary>
        /// Add RouteValueRequestCultureProvider, so localization will be based on route value {culture}
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="cultures">List of supported cultures</param>
        /// <param name="defaultCulture">default culture name</param>
        /// <param name="useAllProviders">true to register all culture providers (Route, QueryString, Cookie, AcceptedLanguageHeader), false to use only Route culture provider.</param>
        /// <returns></returns>
        public static IMvcBuilder ExAddRouteValueRequestCultureProvider(this IMvcBuilder builder, IList <CultureInfo> cultures, string defaultCulture, bool useAllProviders)
        {
            builder.Services.Configure <RequestLocalizationOptions>(ops =>
            {
                if (useAllProviders)
                {
                    ops.RequestCultureProviders.Insert(0, new RouteSegmentCultureProvider(cultures, defaultCulture));
                }
                else
                {
                    ops.RequestCultureProviders.Clear();
                    ops.RequestCultureProviders.Add(new RouteSegmentCultureProvider(cultures, defaultCulture));
                }

                _providers = ops.RequestCultureProviders;
                _cultures  = cultures;
            });

            builder.AddRazorPagesOptions(x =>
            {
                x.Conventions.Add(new RouteTemplateModelConvention());
            });

            return(builder);
        }
コード例 #5
0
ファイル: Startup.cs プロジェクト: codesulting/Cache
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseSqlite(Configuration.GetConnectionString("CacheContext")));

            services.AddDefaultIdentity <IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddRoles <IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>();

            IMvcBuilder builder = services.AddRazorPages();

            builder.AddRazorPagesOptions(options =>
            {
                options.Conventions.AddPageRoute("/Firearms/Index", "");
            });
#if DEBUG
            if (Environment.IsDevelopment())
            {
                builder.AddRazorRuntimeCompilation();
            }
#endif

            services.AddControllers(config =>
            {
                var policy = new AuthorizationPolicyBuilder()
                             .RequireAuthenticatedUser()
                             .Build();
                config.Filters.Add(new AuthorizeFilter(policy));
            });
        }
コード例 #6
0
        };//Admin比较特殊,不需要全部输出

        public IMvcBuilder AuthorizeConfig(IMvcBuilder builder, IWebHostEnvironment env)
        {
            //鉴权配置
            //添加基于Cookie的权限验证:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-2.1&tabs=aspnetcore2x
            builder.Services
            .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(AdminAuthorizeAttribute.AuthenticationScheme, options =>
            {
                options.AccessDeniedPath = "/Admin/Forbidden/";
                options.LoginPath        = "/Admin/Login/";
                options.Cookie.HttpOnly  = false;
            });
            builder.Services
            .AddAuthorization(options =>
            {
                options.AddPolicy("AdminOnly", policy =>
                {
                    policy.RequireClaim("AdminMember");
                });
            });

            builder.AddRazorPagesOptions(options =>
            {
                options.Conventions.AuthorizePage("/", "AdminOnly"); //必须登录
                options.Conventions.AllowAnonymousToPage("/Login");  //允许匿名
                                                                     //更多:https://docs.microsoft.com/en-us/aspnet/core/security/authorization/razor-pages-authorization?view=aspnetcore-2.2
            });

            SenparcTrace.SendCustomLog("系统启动", "完成 Area:Admin 注册");

            return(builder);
        }
コード例 #7
0
 public static IMvcBuilder SetAuthorizePage(this IMvcBuilder mvcBuilder, string authorizeUrl = "/Account/Login")
 {
     mvcBuilder.AddRazorPagesOptions(options =>
     {
         options.Conventions.AuthorizePage(authorizeUrl);
     });
     return(mvcBuilder);
 }
コード例 #8
0
 public void ConfigureMvc(IMvcBuilder builder)
 {
     builder.AddRazorPagesOptions(options =>
     {
         options.RootDirectory = "/Pages";
         options.AuthorizeFolder("/Auth");
     });
 }
コード例 #9
0
        public static IMvcBuilder AddModularRazorPages(this IMvcBuilder builder)
        {
            builder.AddRazorPagesOptions(options =>
            {
                options.RootDirectory = "/";
                options.Conventions.Add(new DefaultModularPageRouteModelConvention());
            });

            return(builder);
        }
コード例 #10
0
        public IMvcBuilder AuthorizeConfig(IMvcBuilder builder, IHostEnvironment env)
        {
            builder.AddRazorPagesOptions(options =>
            {
                //此处可配置页面权限
            });

            SenparcTrace.SendCustomLog("Message 启动", "完成 Area:WorkShop.Xncf.Message 注册");

            return(builder);
        }
コード例 #11
0
        public IMvcBuilder AuthorizeConfig(IMvcBuilder builder, IWebHostEnvironment env)
        {
            builder.AddRazorPagesOptions(options =>
            {
                //此处可配置页面权限
            });

            SenparcTrace.SendCustomLog("系统启动", "完成 Area:MyApp 注册");

            return(builder);
        }
コード例 #12
0
        public IMvcBuilder AuthorizeConfig(IMvcBuilder builder, IHostEnvironment env)
        {
            builder.AddRazorPagesOptions(options =>
            {
                //此处可配置页面权限
            });

            SenparcTrace.SendCustomLog("Template_XncfName 启动", "完成 Area:Template_OrgName.Xncf.Template_XncfName 注册");

            return(builder);
        }
コード例 #13
0
        /// <summary>
        /// Add RouteValueRequestCultureProvider, so localization will be based on route value {culture}
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="cultures">List of supported cultures</param>
        /// <param name="defaultCulture">default culture name</param>
        /// <returns></returns>
        public static IMvcBuilder ExAddRouteValueRequestCultureProvider(this IMvcBuilder builder, IList <CultureInfo> cultures, string defaultCulture)
        {
            builder.Services.Configure <RequestLocalizationOptions>(ops => {
                ops.RequestCultureProviders.Insert(0, new RouteValueRequestCultureProvider(cultures, defaultCulture));
            });

            builder.AddRazorPagesOptions(x =>
            {
                x.Conventions.Add(new RouteTemplateModelConvention());
            });

            return(builder);
        }
コード例 #14
0
        /// <summary>
        /// Adds PineBlog Razor Pages services to the services collection.
        /// </summary>
        /// <param name="builder">The Microsoft.Extensions.DependencyInjection.IMvcBuilder.</param>
        public static IMvcBuilder AddPineBlogRazorPages(this IMvcBuilder builder)
        {
            ConfigureServices(builder.Services);

            builder.AddApplicationPart(typeof(Controllers.FileController).Assembly);
            builder.AddRazorPagesOptions(SetRazorPagesOptions);
            builder.AddMvcOptions(options =>
            {
                options.Filters.Add <PineBlogViewDataAsyncPageFilter>();
            });
            builder.AddFluentValidation();

            return(builder);
        }
コード例 #15
0
 public static IMvcBuilder AddPiranhaManagerOptions(this IMvcBuilder builder)
 {
     return(builder
            .AddRazorPagesOptions(options => {
         options.Conventions.AuthorizeAreaFolder("Manager", "/");
         options.Conventions.AllowAnonymousToAreaPage("Manager", "/login");
     })
            .AddViewLocalization()
            .AddDataAnnotationsLocalization()
            .AddJsonOptions(options =>
     {
         options.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
     }));
 }
コード例 #16
0
        public static IMvcBuilder AddUrlRequestLocalization(this IMvcBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.AddRazorPagesOptions(options =>
            {
                options.Conventions.Add(new CultureTemplatePageRouteModelConvention());
            });

            builder.Services.AddCultureTemplatePage();

            return(builder);
        }
コード例 #17
0
        /// <summary>
        /// Adds PineBlog Razor Pages services to the services collection.
        /// </summary>
        /// <param name="builder">The Microsoft.Extensions.DependencyInjection.IMvcBuilder.</param>
        public static IMvcBuilder AddPineBlogRazorPages(this IMvcBuilder builder)
        {
            ConfigureServices(builder.Services);

            builder.AddApplicationPart(typeof(Controllers.FileController).Assembly);
            builder.AddRazorPagesOptions(option =>
            {
                option.Conventions.AuthorizeAreaFolder("Admin", "/");
                option.Conventions.AddAreaPageRoute("Blog", "/Post", PineBlogConstants.BlogAreaPath + "/{*slug}");
            });
            builder.AddMvcOptions(options =>
            {
                options.Filters.Add <PineBlogViewDataAsyncPageFilter>();
            });
            builder.AddFluentValidation();

            return(builder);
        }
コード例 #18
0
    public static IMvcBuilder AddPiranhaManagerOptions(this IMvcBuilder builder,
                                                       Action <MvcNewtonsoftJsonOptions> jsonOptions = null)
    {
        return(builder
               .AddRazorPagesOptions(options =>
        {
            options.Conventions.AuthorizeAreaFolder("Manager", "/");
        })
               .AddViewLocalization()
               .AddDataAnnotationsLocalization()
               .AddNewtonsoftJson(options =>
        {
            // Invoke custom json options
            jsonOptions?.Invoke(options);

            // Set required options for Piranha
            options.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
        }));
    }
コード例 #19
0
        public static IServiceCollection AddConfiguredMvc(this IServiceCollection services)
        {
            var parameterTransformer = new SlugifyParameterTransformer();

            IMvcBuilder mvc = services.AddMvc(options =>
            {
                var convention = new RouteTokenTransformerConvention(parameterTransformer);
                options.Conventions.Add(convention);
            });

            mvc.AddRazorPagesOptions(options =>
            {
                var convention = new PageRouteTransformerConvention(parameterTransformer);
                options.Conventions.Add(convention);
            });

            mvc.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
            mvc.AddDataAnnotationsLocalization(options =>
            {
                options.DataAnnotationLocalizerProvider = (type, factory) => factory.Create(typeof(SharedResources));
            });

            return(services);
        }