2013-06-12 7 views
0

코드에서 리디렉션 루프의 원인을 알고 있습니다. 어떻게 수정해야할지 모르겠습니다. 첫째, 내 코드.리디렉션 루프 해결

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를 추가하여 사용자가 액세스 권한이 없다는 다른보기를 표시 할 수 있습니다. 나는 이것이 리다이렉트 루프가 어디서오고 있는지 확신한다. 누구든지 리디렉션 루프를 제거 할 수있는 방법에 대한 아이디어가 있습니까? Request.Url.ToString()을 시도한 다음 !var.Contains을 시도하여 리디렉션을 수행했지만 그 작업을 수행 할 수도 없었습니다.

편집 : 누군가가 Response.Redirect()에 대한 대안이 있다면 듣고 싶습니다. 작동하지만, 원래는 Response.End()을 사용하고 있었고 코드 실행을 허용하지 않았기 때문에 Response.Redirect()QueryString을 사용하여 원하는대로 할 수있었습니다.

답변

1

사용자가 활성 상태인지 여부는 두 번입니다. 또한 두 번째 검사에서 페이지를 계속 리디렉션하여 검사를 계속합니다.

switch (Request.QueryString["Error_ID"]) 
{ 
(...) 
case "2": 
    // Error Code 2 is when a user is not currently Active. 
    MultiView1.ActiveViewIndex = 2; 
    break; 
(...) 

와 두 번째 검사는 여기에 있습니다 : :

첫 번째 검사는 여기

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 acces. 
     Response.Redirect("default.aspx?Error_ID=2"); 
    } 

그래서 두 번째 검사 자체에 페이지를 리디렉션, 그리고 영원히 반복 유지합니다. ERROR_ID 경우

1) 페이지의 실행을 중지

이 문제를 해결하는 가장 쉬운 방법, IMHO은, 당신도 할 수 즉, 귀하의 오류 코드가 "2"인 경우 현재 사용자가 활성화되어 있는지 확인하지 않는 것입니다 ERROR_ID가 2 인 경우, 즉에 두 번째 체크 변경,

case "2": 
    // Error Code 2 is when a user is not currently Active. 
    MultiView1.ActiveViewIndex = 2; 
    Response.End(); // <--- this will stop the execution before reaching the first block 
    break; 

2) 페이지를 다시 리디렉션하지 마십시오 : 2, 즉에 먼저 확인을 변경

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 acces. 

    if (MultiView1.ActiveViewIndex != 2) { // check if the page has already been redirected 
     Response.Redirect("default.aspx?Error_ID=2"); 
    } 
} 

이럴 솔루션이 보인다 cl가되다. eanest와 어떻게 이런 일을하고 약 두

+0

완벽하고 간단합니다. 고맙습니다. 나는 해결책 2와 함께 갔다. 그리고 설명에 감사드립니다. 그게 문제가 어디에서 왔는지 알았지 만, 어떻게 그리고 왜 제대로 파악하지 못했습니다. – Trido

1

의 가장 우아한 :

if(MultiView1.ActiveViewIndex != 2) 
{ 
    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 acces. 
     Response.Redirect("default.aspx?Error_ID=2"); 
     } 
    } 
    } 
} 
1
쿼리 문자열 값 (ERROR_ID)는 방법 1 또는 2가 아닌 경우에만 데이터베이스 검사를 수행 할 필요가

로직이 작성되면 사용자가 활성 상태인지 여부를 항상 확인하고 그렇지 않은 경우 Error_ID = 2 쿼리 문자열 값을 페이지에 계속 보내면 루프에 걸릴 수 있습니다. 쿼리 문자열을 별도의 메서드로 테스트하기위한 로직을 분리하고 데이터베이스에서 활성 값을 쿼리할지 여부를 불리언 값으로 반환하는 것이 좋습니다.

+0

현재 방법을 세 가지 별도의 방법으로 나누는 것이 좋습니다. HandleQueryString, GetCurrentUserName 및 IsUserActive입니다.여기서 한 가지 방법으로 너무 많이하고 있습니다. 아마도이 문제를 해결하는 방법의 혼란의 일부가 어디에서 오는지 알 것입니다. 로직을 분리함으로써 기능을 여러 조각으로 나누고 잠재적으로 다른 곳에서 로직을 재사용 할 수 있습니다. 예를 들어 데이터 액세스 계층 (DAL)에 데이터베이스 논리를 넣은 다음 필요할 경우 다른 .aspx 페이지에서이 IsUserActive 메서드를 활용할 수 있습니다. –