【问题标题】:asp.net membership - how to determine programmatically is user is in roleasp.net 成员资格 - 如何以编程方式确定用户是否在角色中
【发布时间】:2010-11-15 21:13:44
【问题描述】:

确定用户是否属于某个角色的代码是什么?

我已经通过 ASP.NET 配置安全选项卡设置了所有用户,但现在想在一些关键区域周围放置逻辑,以便只有特定角色的人才能看到和访问这些区域。

【问题讨论】:

    标签: asp.net membership roles


    【解决方案1】:

    感谢“Chris Van Opstal”。我这样解决了我的问题,

        public ActionResult Index()
        {
    
            if (User.IsInRole("Supervisor"))
            {
                return RedirectToAction("Index", "InvitationS");
            }
            return View();
        }
    

    【讨论】:

      【解决方案2】:

      查看Roles 类,特别是 IsUserInRole、GetUsersInRole、AddUserToRole 等。

      我一直在使用这些。

      【讨论】:

        【解决方案3】:

        简单~

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

        【讨论】:

        • 此代码将查看 sql 数据库(这是我的提供者)以确定在 asp.net 中的角色?
        • 是的,它将查看您配置的任何提供程序。
        【解决方案4】:
        if (User.IsInRole("rolename")) {
          // my action
        }
        

        【讨论】:

        • User 是 Page 和 HttpContext 类的一个属性,所以你可以在页面上简单地作为 User 访问它,或者在非页面文件中作为 HttpContext.Current.User 访问它。 MSDN 上的更多信息:msdn.microsoft.com/en-us/library/…
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-31
        • 1970-01-01
        相关资源
        最近更新 更多