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

问题描述 投票:0回答:3

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

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

authentication asp.net-mvc-5 kentico kentico-12
3个回答
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);
                }
        }

    }

0
投票

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


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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.