我知道是什么导致了我的代码中的重定向循环,我只是不确定如何修复它。首先,我的代码。
switch (Request.QueryString["Error_ID"])
{
case "1":
// Error Code 1 is when a user attempts to access the Admin section and does not have rights to.
MultiView1.ActiveViewIndex = 1;
break;
case "2":
// Error Code 2 is when a user is not currently Active.
MultiView1.ActiveViewIndex = 2;
break;
default:
// Default is View Index 0 for default access.
MultiView1.ActiveViewIndex = 0;
break;
}
// Get current username.
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
// Test to see if user is Active.
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HSEProjRegConnectionString1"].ConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT [active] FROM [tbl_Person] WHERE username LIKE @username", conn))
{
cmd.Parameters.AddWithValue("@username", "%" + userName + "%");
var res = cmd.ExecuteScalar();
bool registeredAndActive = (bool)res;
if (registeredAndActive)
{
// Active Condition. The DEFAULT in SWITCH() will take care of displaying content.
}
else
{
// !Active Condition. Shows an alternative version of the default page where the user is told they do not have access.
Response.Redirect("default.aspx?Error_ID=2");
}
}
代码的重点是首先检查 SWITCH() 方法中的查询字符串,以防稍后页面提供。然后它获取当前登录的 AD 用户名,然后检查用户数据库以查看用户是否标记为活动。如果是这样,它什么也不做,因为它将允许页面正常加载。如果没有,那么它会重定向到同一页面,但会附加一个 Error_ID,以便我可以显示一个不同的视图,说明用户无权访问。我很确定这就是重定向循环的来源。有人对如何消除重定向循环有任何想法吗?我尝试做 aRequest.Url.ToString()
然后 a!var.Contains
做重定向,但我也无法做到这一点。
编辑:我应该注意,我很想知道是否有人可以替代Response.Redirect()
. 它可以工作,但最初,我正在使用Response.End()
并且不允许任何代码运行,所以想出了 usingResponse.Redirect()
和 aQueryString
来做我想做的事。