【问题标题】:User.IsInRole errorUser.IsInRole 错误
【发布时间】:2011-08-20 14:43:02
【问题描述】:

我在Site.Master.csPage_Load 中有这段代码。

if(User.IsInRole("Read"))
{
   NavigationMenu.Visible = false;
}

我得到这个错误:

非静态字段、方法或 财产 'Microsoft.VisualBasic.ApplicationServices.User.IsInRole(string).

有什么线索吗?

【问题讨论】:

    标签: c# asp.net authorization


    【解决方案1】:

    您可以获得当前的HttpContextuser 并使用IsInRole method 验证给定角色,如下所示。

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

    改变你的方法为

    if(HttpContext.User.IsInRole("Read"))
    {
       NavigationMenu.Visible = false;
    }
    

    【讨论】:

      【解决方案2】:

      看起来您正在使用该类而不是该类的实例尝试:

      User user = new User();
      user.IsInRole("Read");
      

      【讨论】:

        【解决方案3】:

        我不完全同意这个答案。您正在从母版页中的 Page 属性获取 User 实例,因此您应该使用:

        var user = Page.User;
        user.IsInRole("your role");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-08-02
          • 1970-01-01
          • 2013-03-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-24
          • 1970-01-01
          相关资源
          最近更新 更多