Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法根据ASP.NET/C中的用户角色隐藏HTML链接#_C#_Webforms - Fatal编程技术网

C# 无法根据ASP.NET/C中的用户角色隐藏HTML链接#

C# 无法根据ASP.NET/C中的用户角色隐藏HTML链接#,c#,webforms,C#,Webforms,我正在使用Visual Studio 2010为我的学校项目开发一个ASP.net网站,我正在应用基于角色的安全性 我想根据用户角色隐藏id=“HyperLink1”的html元素,但是,我想隐藏的链接仍然显示错误的凭据。以下是我的母版页代码背后的一段代码: protected void Page_Load(object sender, EventArgs e) { footerYear.Text = DateTime.Now.Year.ToString(

我正在使用Visual Studio 2010为我的学校项目开发一个ASP.net网站,我正在应用基于角色的安全性

我想根据用户角色隐藏id=“HyperLink1”的html
  • 元素,但是,我想隐藏的链接仍然显示错误的凭据。以下是我的母版页代码背后的一段代码:

        protected void Page_Load(object sender, EventArgs e)
            {
            footerYear.Text = DateTime.Now.Year.ToString();
    
            if (!IsPostBack)
            {
                if (Context.User.Identity.IsAuthenticated)
                {
                    if (Context.User.IsInRole("student"))
                    {
                       HyperLink1.Visible = false;
                        Label1.Text = "Hi " + Context.User.Identity.Name + "!";
               }
    
                    else if (Context.User.IsInRole("teacher"))
                    {
                        HyperLink1.Visible = false;
                        Label1.Text = "Hi " + Context.User.Identity.Name + "!";
    
                    }
    
                    else if (Context.User.IsInRole("registrar"))
                    {
                        Label1.Text = "Hi " + Context.User.Identity.Name + "!";
                        HyperLink1.Visible = false;
                    }
                }
    
                else if (!Context.User.Identity.IsAuthenticated)
                {
                    Label1.Text = "Hi Guest!";
                }
    
            } 
    

    我建议您简化代码并翻转条件:

      protected void Page_Load(object sender, EventArgs e) {
        Label1.Text = Context.User.Identity.IsAuthenticated ? "Hi " + Context.User.Identity.Name + "!" : "Hi Guest!";
        footerYear.Text = DateTime.Now.Year.ToString();
        HyperLink1.Visible = false;
        if (Context.User.Identity.IsAuthenticated) {
          HyperLink1.Visible = !Context.User.IsInRole("student") && !Context.User.IsInRole("teacher") && !Context.User.IsInRole("registrar");
        }
      }
    

    debug debug,一步一步地调试…对于用户y的哪个角色,您面临问题?例如,对于学生角色,我想隐藏id=Hyperlink1的html链接,此外,如果用户是来宾(即未经身份验证),我还想隐藏链接。您调试了代码吗?它是否正确执行?是的,没有错误,但即使在来宾用户模式下,链接仍会显示。