用户登录网站时如何重定向用户?

时间:2019-09-03 11:03:23

标签: asp.net

当用户登录我的网站时,我需要将其重定向到特定页面。

在我的Web.Config文件中,我有:

<configuration>
<location path="User_personal_a.aspx">
  <system.web>
    <authorization>
      <allow users="User1" />
      <deny users="*" />
    </authorization>
  </system.web>
</location>

然后当他们登录时我有:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (User.Identity.IsAuthenticated)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
                {
                    Response.Redirect("UnauthorizedAccess.aspx");
                }
                if (UserName.Text.Trim() == "User1")
                {
                    Response.Redirect("Client Reports/User_personal_a.aspx");
                }
                else
                {
                    Response.Redirect("Admin/Dashboard.aspx");
                }
                StatusText.Text = string.Format("Hello {0}!!",
                User.Identity.GetUserName());
                LogoutButton.Visible = true;
            }
            else
            {
                LoginForm.Visible = true;
            }
        }
    }

但是不幸的是,这不起作用:-(

对此需要帮助。

谢谢

1 个答案:

答案 0 :(得分:0)

肯定存在错误/错误的设计

这部分

if (UserName.Text.Trim() == "User1")

始终为假,因为文本框UserName可能为空-特别是在到达该行代码之前设置的回发之前。

您最有可能寻找的地方必须是

if (HttpContext.Current.User.Identity.Name == "User1")

但这是您代码逻辑的基础。

相关问题