0

在 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 字段似乎是我需要的属性,并且在以前的版本中使用过,但我找不到在新管理面板上设置的方法。

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

4

3 回答 3

0

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

我的整个 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);
                }
        }

    }
于 2019-04-04T20:38:41.867 回答
0

我最终使用了一个带有界面的自定义表,供用户设置页面是否需要身份验证。由于这是对 OnAuthentication 的覆盖,因此每个页面都会调用此方法。我希望使用内置的 Kentico 功能有更好的解决方案。这是最终代码:

protected override void OnAuthentication(AuthenticationContext filterContext)
    {
        base.OnAuthentication(filterContext);

        var routePath = filterContext.HttpContext.Request.Path;
        var allowAccess = Authentication.CanEnterPage(filterContext.Principal.Identity.IsAuthenticated, routePath);
        if (!allowAccess)
        {
            filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary(new { controller = "Account", action = "Signin", returnUrl = routePath })
            );
        }
    }   

下面的静态方法包含访问页面的逻辑:

public static bool CanEnterPage(bool isAuthenticated, string routePath)
    {
        var page = DocumentHelper.GetDocuments().Path(routePath).FirstOrDefault();

        if (page == null)
            return false;

        var pageAccess = PageAccessInfoProvider.GetPageAccesses()
            .WhereEquals("PageAccessNodeID", page.NodeID).FirstOrDefault();

        // Create a record if pageAccess is null
        if (pageAccess == null)
        {
            pageAccess = CreateRecordsPageAccess(page);
        }

        var isSecure = pageAccess.PageAccessHasAuthentication;
        var allowAccess = isSecure && isAuthenticated || !isSecure;
        return allowAccess;
    }
于 2019-04-30T16:48:01.870 回答
0

您可以使用授权实时站点操作文档中提到的方法。

于 2019-04-05T08:09:24.453 回答