加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何根据用户的角色创建具有不同显示的视图?

发布时间:2020-12-15 23:10:42 所属栏目:asp.Net 来源:网络整理
导读:我想根据用户所在的角色创建一个具有不同显示的视图. 我应该为不同的角色创建不同的视图,还是应该检查Veiw页面本身而不是在动作中的角色? 我如何查看视图页面上的角色? 解决方法 Or should i use check the roles on the Veiw page its self rather than o
我想根据用户所在的角色创建一个具有不同显示的视图.

我应该为不同的角色创建不同的视图,还是应该检查Veiw页面本身而不是在动作中的角色?

我如何查看视图页面上的角色?

解决方法

Or should i use check the roles on the
Veiw page its self rather than on
actions,if so can someone plz show me
how do check that on view page

你需要做这两个.检查操作上的角色作为安全措施,并检查视图上的角色以启用/禁用特定控件.

在您的视图页面中,检查角色的长形式是

HttpContext.Current.User.IsInRole("Administrator")

许多开发人员将创建页面助手方法,以便最终可以为您的应用程序提供更简洁的东西

public static bool IsAdmin(this ViewUserControl pg)
{
    return pg.Page.User.IsInRole("Administrator")
}

那么在你看来你可以使用this.IsAdmin()

为了保持你的视野混乱,看看使用部分视图

<% if (IsAdmin())
   {
      Html.RenderPartial("AdminPanel");
   }
   else
   {
      Html.RenderPartial("UserPanel");
   }
%>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读