首页 > 解决方案 > Kentico 12 - 如何将单个页面设置为需要身份验证?

问题描述

在 Kentico 12 上,页面内的属性 Security 不像以前的版本Kentico 11 - Interface Access那样具有 Access 字段。

我需要提供此功能,因此我正在考虑使用这样的覆盖 OnAuthentication 方法:

protected override void OnAuthentication(AuthenticationContext filterContext)
    {
        var isAuthenticated = filterContext.Principal.Identity.IsAuthenticated;
        var routePath = filterContext.HttpContext.Request.Path;
        var page = DocumentHelper.GetDocuments().Path(routePath).FirstOrDefault();
        var allowAccess = (page.HasSecureProperty && isAuthenticated) || !page.HasSecureProperty;
        if (allowAccess)
        {
            base.OnAuthentication(filterContext);
        }
        else
        {
            filterContext.Result = new RedirectToRouteResult(
                  new RouteValueDictionary(new { controller = "Account", action = "Signin" })
            );
        }
    }

HasSecureProperty 将是 kentico 页面中管理员或编辑用户可以在管理面板上设置的属性。我计划使用自定义表创建此属性,并在页面上为用户创建一个界面。

CMS_Tree 上的 IsSecureNode 字段似乎是我需要的属性,并且在以前的版本中使用过,但我找不到在新管理面板上设置的方法。

是否有其他解决方案允许用户在页面上设置身份验证?我担心性能,因为每个动作都会调用这个方法。谢谢你。

标签: authenticationasp.net-mvc-5kenticokentico-12

解决方案


我做过类似的事情,所以也许它会帮助你指出正确的方向。

我的整个 MVC 站点都需要身份验证,所以这可能是不同的地方。在 MVC 中,当我想获取文件并检查权限时,我会执行以下操作:

var files = subcat.Children.WithAllData.WithPermissionsCheck;

在 CMS 方面,我在页面类型上有一个字段,允许用户选择角色,另一个字段用于选择用户。然后我在文档更新或插入上有一个自定义事件来更新设置。

这是我用于更新 ACL 的代码:

private void UpdateSettings(TreeNode node)
    {
        ObjectQuery<RoleInfo> roles = null;
        ObjectQuery<UserInfo> users = null;
        var columnRoles = node.GetStringValue("Roles", "");
        if (columnRoles != "")
        {
            var rolesConcat = columnRoles.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            var where = "RoleName IN " + "('" + string.Join("','", rolesConcat) + "')";
            EventLogProvider.LogInformation("Document Event", "Roles", where);
            roles = RoleInfoProvider.GetRoles()
                .Where(where);
        }

        var columnUsers = node.GetStringValue("Users", "");
        if (columnUsers != "")
        {
            var usersConcat = columnUsers.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            var where = "UserName IN " + "('" + string.Join("','", usersConcat) + "')";
            EventLogProvider.LogInformation("Document Event", "Users", where);
            users = UserInfoProvider.GetUsers()
                .Where(where);
        }

        if (node != null)
        {
            // Gets the ID of the ACL item that stores the page's permission settings
            int nodeACLID = ValidationHelper.GetInteger(node.GetValue("NodeACLID"), 0);

            // Deletes the page's ACL item
            // Removes the page's permission settings for all users and roles
            AclItemInfoProvider.DeleteAclItems(nodeACLID);

            node.IsSecuredNode = true;
            int allowed = DocumentSecurityHelper.GetNodePermissionFlags(NodePermissionsEnum.Read);

            // Prepares a value indicating that no page permissions are denied
            int denied = 0;

            if (users != null)
                foreach (var user in users)
                {
                    // Sets the page's permission for the user (allows the 'Modify' permission)
                    AclItemInfoProvider.SetUserPermissions(node, allowed, denied, user);
                }

            if (roles != null)
                foreach (var role in roles)
                {
                    // Sets the page's permission for the user (allows the 'Modify' permission)
                    AclItemInfoProvider.SetRolePermissions(node, allowed, denied, role);
                }
        }

    }

推荐阅读