orchardcms - 如何从 View 页面访问当前用户在 Orchard 中的角色?

标签 orchardcms orchardcms-1.9

我正在尝试从 View 中获取当前用户在 Orchard 1.9.2 中的角色。

我试过这段代码,但它总是返回 0 个角色:

var roles = ((ContentItem)WorkContext.CurrentUser.ContentItem).As<UserRolesPart>().Roles;

来源:https://www.cloudconstruct.com/blog/retrieving-role-information-in-your-orchard-view-templates

有什么想法吗?

最佳答案

也许缺少 using 语句?

试试下面的代码:

@using Orchard.ContentManagement

@{
  var roles = WorkContext.CurrentUser.As<Orchard.Roles.Models.UserRolesPart>().Roles;
}

编辑1

用户似乎未通过身份验证。上面发布的代码行创建了以下调用:

  1. Orchard.Environment.WorkContextImplementation.GetState("CurrentUser")
  2. Orchard.Environment.WorkContextImplementation.FindResolverForState("CurrentUser")
  3. 枚举IWorkContextStateProvider列表,在这个列表中应该有如下实现:Orchard.Security.CurrentUserWorkContext
  4. Orchard.Security.CurrentUserWorkContext.Get("CurrentUser")
  5. IAuthenticationService.GetAuthenticatedUser() -> 验证后返回用户

所以看起来这些步骤中的任何一个都在没有用户的情况下返回。我建议您在 Orchard.Environment.WorkContextImplementation.GetState() 中创建一个断点以找出问题所在。

关于orchardcms - 如何从 View 页面访问当前用户在 Orchard 中的角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36874409/

相关文章:

orchardcms - 我可以在不借助 HQL 的情况下高效查询通用字段吗?

c# - Orchard CMS 如何在 Orchard CMS 中传输我的 ASP.NET MVC 布局(页眉、页脚等)/主题?

c# - WebApi 路由返回 Not Found in Orchard Module

asp.net-mvc - 为Orchard创建一个模块,用于存储前端数据

orchardcms - 是否可以在非主题 View 中使用公共(public)资源?

asp.net - Orchard CMS-Settings.txt排除了Webdeploy

orchardcms - Orchard 中的迁移按什么顺序执行?

c# - 删除迁移中的 TaxonomyField

orchardcms - 为什么 WorkContext.CurrentUser 总是空的?

orchardcms - 我应该在哪里放置我希望始终为每个请求运行的代码?