【问题标题】:Hide web link from users without role assigned对未分配角色的用户隐藏 Web 链接
【发布时间】:2012-04-04 11:12:07
【问题描述】:

我目前通过使用角色和规则在我的应用程序中实现了一些安全性,但我想知道是否可以根据用户是否属于某个角色来隐藏 Web 链接。

我目前拥有它,因此用户属于“管理员”角色或“用户”角色。在我的应用程序中的文件夹上设置了规则,因此如果用户属于任一组,他们可以单击 Web 链接并将其带到页面。但是,如果新用户注册他们不属于任何角色,并且当他们单击 Web 链接时,它会将他们重定向到主页(参见下面的代码)。这很好,但是,我想知道如果用户不属于特定角色阻止他们点击它,是否可以隐藏该 Web 链接。此外,如果用户不属于任何角色,是否可以将他们重定向到页面以告诉他们需要分配角色?

<authentication mode="Forms">
  <forms loginUrl="~/Homepage1.aspx" timeout="2880" />
</authentication>

我没有在当前应用程序中使用它,但在过去,它会检查用户是否已通过身份验证,我想知道我是否可以使用类似的 IF 语句: 如果 user.identity 不属于 'user' 或 'admin' 角色,则将其重定向到页面并显示错误?

    If User.Identity.IsAuthenticated = True Then

        Dim myUser As MembershipUser
        Dim objUser As Object

        myUser = Membership.GetUser(User.Identity.Name)
        objUser = myUser.ProviderUserKey

        Session("ID") = objUser

    Else

        MsgBox("You are not entitled to view this page", MsgBoxStyle.Critical, "Warning")
        Response.Redirect("~/NoAccess.aspx")

    End If

【问题讨论】:

    标签: asp.net vb.net visual-studio-2010 webforms


    【解决方案1】:

    在您的情况下,我认为 LoginView 控件会更好。请参阅此处的文档:http://msdn.microsoft.com/en-ca/library/system.web.ui.webcontrols.loginview(v=vs.100).aspx

    祝你有美好的一天!

    【讨论】:

      【解决方案2】:

      是的,你可以使用User.IsInRole(role as String)

      <% If User.IsInRole("admin") Then %>
      
      <a href="admin-tools.aspx">Admin Tools</a>
      
      <% End If %>
      

      你明白了。您可以根据任何角色定制它以显示/隐藏链接。

      要了解有关角色管理的更多信息,请阅读here

      【讨论】:

      • 我假设我必须声明用户?暗淡用户作为会员用户?还是有特定的角色声明?
      • User 是内置的。这是当前登录用户的System.Security.Principal.IPrincipal
      • 我建议您阅读我添加到答案中的链接,只是为了了解角色管理及其工作原理。
      猜你喜欢
      • 2012-06-20
      • 2011-12-29
      • 1970-01-01
      • 1970-01-01
      • 2014-05-26
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      • 2021-08-05
      相关资源
      最近更新 更多