|
 |
aspx thread: Response.Redirect, Server.Transfer throws error.
Message #1 by "Matt Dittman" <mddittman@h...> on Fri, 24 May 2002 20:24:46
|
|
I have a C# .aspx login page based from a SQL database... if the user
checks out, redirect them to the application. So far, so good.
I went back and put the database code and the redirect in a try-catch
block, and found that if you call Response.Write or Server.Transfer within
a try-catch block it throws a "System.Threading.ThreadAbortException:
Thread was being aborted" error. Why does this happen? Is there code
you're supposed to execute before the page redirection that terminates the
thread? Or is this (for some strange reason) by design??
code below:
private void ValidateUser(){
try
{
Conn.Open();
Cmd = new OdbcCommand("select * from
tb_ContractorInfo where UserID='" + USID.Text + "'" , Conn);
DR = Cmd.ExecuteReader();
while (DR.Read())
{
if(DR["Enabled"].Equals(false))
{
DR.Close();
Conn.Close();
Response.Redirect
("default.asp?x=b");
}
if(UPWD.Text==DR
["Password"].ToString())
{
//cookie for User ID
HttpCookie newCookie = new
HttpCookie("cooUsrIdty",DR["UserID"].ToString());
//Set the cookie to expire
in 15 days
DateTime dtNow =
DateTime.Now;
TimeSpan tsDays = new
TimeSpan(15, 0, 0, 0);
newCookie.Expires = dtNow
+ tsDays;
Response.Cookies.Add
(newCookie);
//cookie for friendly user
name
HttpCookie newCookie1 =
new HttpCookie("cooUsrName",DR["FirstName"].ToString() + " " + DR
["LastName"].ToString());
Response.Cookies.Add
(newCookie1);
//cookie for user role
HttpCookie newCookie2 =
new HttpCookie("cooUsrRole",DR["Role"].ToString());
Response.Cookies.Add
(newCookie2);
//cookie for first name
HttpCookie newCookieFN =
new HttpCookie("cooUsrFName",DR["FirstName"].ToString());
Response.Cookies.Add
(newCookieFN);
//cookie for last user name
HttpCookie newCookieLN =
new HttpCookie("cooUsrLName",DR["LastName"].ToString());
Response.Cookies.Add
(newCookieLN);
DR.Close();
//Timestamp user in
database:
string strBrw =
Request.ServerVariables["HTTP_USER_AGENT"];
string sql
= "dbo.bfc_spLogInLast '" + USID.Text + "','" + strBrw + "'";
OdbcCommand StoredProc =
new OdbcCommand(sql,Conn);
StoredProc.CommandType =
CommandType.StoredProcedure;
Int32 recordsAffected =
StoredProc.ExecuteNonQuery();
Conn.Close();
Server.Transfer
("allHome.aspx");
strPSD = "Mooo";
}
}
}
catch(Exception x){
ErrorMsg.Text = x.ToString();
strPSD ="Mooo";
//Response.Redirect("default.aspx?x=v");
}
}
Message #2 by "Minh T. Nguyen" <nguyentriminh@y...> on Fri, 24 May 2002 14:51:28 -0700 (PDT)
|
|
Matt,
Can it be that you are having an infinite
recursion? You are redirecting to default.aspx, I see.
Maybe you are not catching the query string parameters
correctly or so and ValidateUser() gets called again
(and again, and again)?
Try redirecting to some unproteted page (like
www.yahoo.com or so) to verify that that's not the
problem.
Second, I believe there is an optional parameter to
the Response.Redirect()/Server.Transfer() method that
tells someone to stop executing the current page. Set
it to false, and see if that helps.
Minh.
--- Matt Dittman <mddittman@h...> wrote:
> I have a C# .aspx login page based from a SQL
> database... if the user
> checks out, redirect them to the application. So
> far, so good.
>
> I went back and put the database code and the
> redirect in a try-catch
> block, and found that if you call Response.Write or
> Server.Transfer within
> a try-catch block it throws a
> "System.Threading.ThreadAbortException:
> Thread was being aborted" error. Why does this
> happen? Is there code
> you're supposed to execute before the page
> redirection that terminates the
> thread? Or is this (for some strange reason) by
> design??
>
> code below:
>
> private void ValidateUser(){
> try
> {
> Conn.Open();
>
> Cmd = new OdbcCommand("select * from
> tb_ContractorInfo where UserID='" + USID.Text + "'"
> , Conn);
>
> DR = Cmd.ExecuteReader();
> while (DR.Read())
> {
> if(DR["Enabled"].Equals(false))
> {
> DR.Close();
> Conn.Close();
> Response.Redirect
> ("default.asp?x=b");
> }
> if(UPWD.Text==DR
> ["Password"].ToString())
> {
> //cookie for User ID
> HttpCookie newCookie = new
> HttpCookie("cooUsrIdty",DR["UserID"].ToString());
> //Set the cookie to expire
> in 15 days
> DateTime dtNow =
> DateTime.Now;
> TimeSpan tsDays = new
> TimeSpan(15, 0, 0, 0);
> newCookie.Expires = dtNow
> + tsDays;
> Response.Cookies.Add
> (newCookie);
> //cookie for friendly user
> name
> HttpCookie newCookie1 =
> new
> HttpCookie("cooUsrName",DR["FirstName"].ToString() +
> " " + DR
> ["LastName"].ToString());
> Response.Cookies.Add
> (newCookie1);
> //cookie for user role
> HttpCookie newCookie2 =
> new HttpCookie("cooUsrRole",DR["Role"].ToString());
> Response.Cookies.Add
> (newCookie2);
> //cookie for first name
> HttpCookie newCookieFN =
> new
>
HttpCookie("cooUsrFName",DR["FirstName"].ToString());
> Response.Cookies.Add
> (newCookieFN);
> //cookie for last user name
> HttpCookie newCookieLN =
> new
> HttpCookie("cooUsrLName",DR["LastName"].ToString());
> Response.Cookies.Add
> (newCookieLN);
>
> DR.Close();
>
> //Timestamp user in
> database:
>
> string strBrw =
> Request.ServerVariables["HTTP_USER_AGENT"];
> string sql
> = "dbo.bfc_spLogInLast '" + USID.Text + "','" +
> strBrw + "'";
> OdbcCommand StoredProc =
> new OdbcCommand(sql,Conn);
> StoredProc.CommandType =
> CommandType.StoredProcedure;
> Int32 recordsAffected =
> StoredProc.ExecuteNonQuery();
> Conn.Close();
> Server.Transfer
> ("allHome.aspx");
> strPSD = "Mooo";
> }
> }
> }
> catch(Exception x){
> ErrorMsg.Text = x.ToString();
> strPSD ="Mooo";
> //Response.Redirect("default.aspx?x=v");
> }
> }
__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com
Message #3 by "Matt Dittman" <mddittman@h...> on Tue, 28 May 2002 14:28:05
|
|
That's not the problem. When you submit the login page, it posts back to
itself, and if it validates the user, you are sent to the application home
page. If the validation fails, the login page reappears along with the
reason the user validation failed. I tried Response.Write AND
Server.Transfer with the optional boolean arguments and they still threw
the exception. I have made sure all my objects were destroyed before the
redirection. The error that is thrown reads "Thread was being aborted".
This occurs regardless of the destination page. If anyone knows what
causes this error, or at least how to get around it, please help!
|
|
 |