authentication - 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 字段似乎是我需要的属性,并且在以前的版本中使用过,但我找不到在新管理面板上设置的方法。
是否有其他解决方案允许用户在页面上设置身份验证?我担心性能,因为每个动作都会调用这个方法。谢谢你。
解决方案
我做过类似的事情,所以也许它会帮助你指出正确的方向。
我的整个 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);
}
}
}
推荐阅读
- javascript - 尝试将 drawImage() 与 webGL HTMLCanvasElement 一起使用会失败并擦除画布?
- kubernetes - 使用 GKE nginx 的 502 错误网关
- arrays - ruby 在多个数组上映射一个函数
- ruby-on-rails - PG::UndefinedTable:错误:关系“step_images”不存在
- r - 多曲面图的特定颜色
- javascript - Javascript检测具有清晰背景的图像上的碰撞
- python - 在类函数中使用“if”语句来升级 Player 类实例
- spring - Spring Boot - 创建涉及多个数据库表的端点 - 最佳实践是什么
- java - Spring Data ElasticSearch(不推荐在搜索请求中指定类型)
- command-line-interface - gcloud dataproc 集群列表过滤器!=