我不应该允许用户 1 通过更改 url 来访问用户 2



我正在使用表单身份验证及其良好性能。但是在user1登录后,他将显示user1.aspx页面,但如果他在登录后更改了url,他也可以访问user2页面,这也不应该发生,所以我在Web配置文件中进行了这样的更改

<authentication mode="Forms">
      <forms
        name=".LOGIN" 
        cookieless="UseCookies"
        loginUrl="LOGIN.aspx"/>
    </authentication>
    <authorization>
      <deny users="?"/>
    </authorization>
  </system.web>

  <location path="~/CabScheduler/User1/User1.aspx">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>

  <location path="~/CabScheduler/User2/User2.aspx">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>


  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

在登录页面中——

 protected void btnLogin_Click(object sender, EventArgs e) 
        { 
            bool validLogin = false; 
            validLogin = IsValidUser(txtUserName.Text.Trim(), txtPassword.Text.Trim());
            int UserId = FindRoleId(txtUserName.Text.Trim(), txtPassword.Text.Trim());
            if (validLogin) 
            {       
                FormsAuthentication.RedirectFromLoginPage(txtUserName.Text.Trim(), false); 
                if(UserId ==1)
                    Response.Redirect("~/User1/User1.aspx");
                else
                    Response.Redirect("~/User2/User2.aspx");
        }

            else 
                lblInformation.Text = "Incorrect Login Information"; 
        } 

告诉我我错过了什么或做错了什么。谢谢!

将 userId 存储在Session中,并检查该 id 对于这些页面上的当前页面是否有效。

Session["userId"] = UserId;

在页面本身:

if (Session["userId"] != xxx)
    Redirect("somewhere");

请填写适当的值。

在 OnLoad 事件User1.aspx检查当前用户是否访问此页面并相应地处理。

在用户 1 上.aspx加载事件检查:

 if(UserId !=1)
  {
    //redirect to login page
  }

请尝试以下操作

 <location path="~/CabScheduler/User2/User2.aspx">
    <system.web>
      <authorization>
                <allow users="user2" />
                <deny users="*, ?" />
      </authorization>
    </system.web>
  </location>

    <location path="~/CabScheduler/User1/User1.aspx">
        <system.web>
            <authorization>
                <allow users="user1" />
                <deny users="*, ?" />
            </authorization>
        </system.web>
    </location>

最新更新