Kentico 12-如何设置单个页面以要求认证?

时间:2019-04-04 18:43:41

标签: authentication asp.net-mvc-5 kentico kentico-12

在Kentico 12上,页面内的“安全性”属性不像以前的版本Kentico 11 - Interface 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似乎是我需要的属性,并且已在以前的版本中使用过,但是我找不到在新的管理面板上进行设置的方法。

是否存在另一种允许用户在页面上设置身份验证的解决方案?我担心性能,因为此方法将在每次操作时调用。谢谢。

3 个答案:

答案 0 :(得分:0)

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

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

var files = subcat.Children.WithAllData.WithPermissionsCheck;

在CMS端,我在页面类型上有一个字段,该字段允许用户选择角色,而另一个则用于选择用户。然后,我在文档更新或插入上插入了custom event,以更新设置。

这是我用于更新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);
                }
        }

    }

答案 1 :(得分:0)

您可以使用authorizing live site actions文档中提到的方法。

答案 2 :(得分: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;
    }