开发者

redirect request doesnt complete after removing session or session.abandon

I am facing a peculiar problem in redirecting after Session.Abandon, here is the code snippet

if (Session["Login"] != null)
            {
                login = (LoginState)Session["Login"];
                if (!RoleValidation.ManagerRoleValidate(login.role_id))
                {
                    Session.Remove("Login");
                    Session.RemoveAll();
                    Session.Abandon();                       
                    Response.Redirect("~/Login.aspx");

                }
            }

here if the condition is true it takes ages to redirect ... and actually it doesnt redirect to the page at all as if it seems its stuck somewhere ... have put this section into try catch block it says as

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

I am not sure what's causing it. What is the reason why it's happening?

The complete code snippet is :

     protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["Login"] != null)
        {
            login = (LoginState)Session["Login"];
            if (!RoleValidation.ManagerRoleValidate(login.role_id))
            {
                Session.Remove("Login");
                Session.RemoveAll();
                Session.Abandon();
                Response.Re开发者_如何学Godirect("~/Login.aspx", false);

            }
        }
        else
        {
            Response.Redirect("~/Login.aspx");
        }


    }
public static Boolean ManagerRoleValidate(String Role_id) 
{
    if (Role_id.Equals("1") || Role_id.Equals("2") || Role_id.Equals("4") || Role_id.Equals("999"))
    {
        return true;
    }
    else
    {
        return false;

    }
}

and there are no redirects in the Login.aspx so that it can redirect again and again.


Possibly the reason might be from Login.aspx also your this portion of code is executing, so this response will redirect to the Login.aspx again and again.


not sure why this caused the trouble . i have replaced the

session.abandon

with

session.clear

method which clears all the session . although in global.asax i have written

void Session_End(object sender, EventArgs e) 
{
    if (Request.Cookies["ASP.NET_SessionId"] != null)
    {
        Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddYears(-30);
    }

    Response.Redirect("~/Login.aspx"); 
}

after removing the line

Response.Redirect("~/Login.aspx"); 

from the above code it should have raised the event session_end inside global.asax but for some reason it took ages to compelte the request . so thats how i solved my problem . if someone can please explain what has happned here and how the above thing solved it . please do reply so that other people can benefit from it


There is another request in the process and in the mean time you want another request. If you add false, it will terminate the current request and go to login page.

Response.Redirect("~/Login.aspx", false);

This problem is caused most often if you add Response.Redirect Statement under Try Catch Block.

try
    {
        Response.Redirect(""~/Login.aspx"); // here you want to redirect, but try catch is still have to complete, It will go in the Exception block
    }
    catch (Exception ex)
    {
        throw;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜