Reputation: 2832
I'm developing web site using asp.net. In my web site after login i set session for the logged user for my web site need. and on each page i checked whether that session is null or having value and depending on the value i redirected to the login page.
My issue is that after login i want to redirect to the previously browsed web page and i done it by using following way-
code on the particular page for checking session value-
if (Session["EmployeeID"] != null)
{
}
else
{
Response.Redirect(ResolveUrl("~/login.aspx?IsTimeout=1"), true);
}
code on the login page-
If Request.QueryString("IsTimeout") IsNot Nothing Then
If Request.QueryString("IsTimeout") = "1" Then
Login1.DestinationPageUrl = Request.UrlReferrer.ToString()
End If
End If
Is there any other way to do this in correct way?
Upvotes: 1
Views: 5100
Reputation: 1038
In my humble opinion I think the best solution to that issue is working in a centralized page in your project .. and according to that, we will not find a suitable page more than our project's Master Page to do that job.I will write some code to show how can we implement that issue :
// In Login Page ..
// I used a submit button to figure out the event of my code ..
protected void ButtonInsert_Click(object sender, EventArgs e)
{
Session.Add("EmployeeID", 100); // This 100 is just a fixed value ..
if (!String.IsNullOrEmpty(Request.QueryString["Red_Page"]))
{
Response.Redirect(Request.QueryString["Red_Page"]);
}
else
{
Response.Redirect("EmployeeDepartment.aspx");
}
}
// In Master Page ..
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// In Master Page
if (Session["EmployeeID"] != null)
{
// Write ur business Code
}
else
{
// I stored the Page URL in uery String.
// Stroing the URL in a cookie will be also a good alternative solution ..
string CurrentURLBeofreTimeout = Request.RawUrl;
string LoginURL = "login.aspx";
string newURL = string.Format("{0}?Red_Page={1}", LoginURL, CurrentURLBeofreTimeout);
Response.Redirect(newURL);
}
}
}
Upvotes: 0
Reputation: 1876
Is there any other way to do this in correct way?
Well I would argue there is. Don't rewrite the authentication, timeouts and redirect when ASP.NET offers the .NET Forms Authentication. If the session times out, it will automatically add the 'ReturnUrl' that you can then handle in your authentication code. Check out this link for source code and discussion on implementing Forms Authentication as well as leveraging some its other capabilities...
Upvotes: 0
Reputation: 18290
Save previous page url to view state when page redirected from another page and when postback generate then redirect to the last page..
Try this:
protected void Page_Load(object sender, EventArgs e)
{
if( !IsPostBack )
{
ViewState["RefUrl"] = Request.UrlReferrer.ToString();
}
if (Session["EmployeeID"] != null)
{
}
else
{
object refUrl = ViewState["RefUrl"];
if (refUrl != null)
Response.Redirect((string)refUrl);
}
}
Ref:
How to go back to the previous page in ASP.NET 2.0
Asp.net forum - redirect to previous page RSS
Upvotes: 0
Reputation: 733
You can accomplish this by using Request.UrlReferrer.ToString(). That will give you the name of the last page the user was on. So then you can do a response.redirect(Request.UrlReferrer.ToString()) or something like that. Sorry for the pseudo-code.
Upvotes: 0
Reputation: 1058
I'm not sure about the correct way... but im doing it something like this..
I have a function that request a login page and passes a parameter of the referrer
protected void RequestLogin()
{
string OriginalUrl = HttpContext.Current.Request.RawUrl;
string LoginPageUrl = "~/LogIn.aspx";
HttpContext.Current.Response.Redirect(String.Format("{0}?ReturnUrl={1}",
LoginPageUrl, OriginalUrl));
}
and here is the redirection after log in.
if (this.Request.QueryString["ReturnUrl"] !=null)
{
this.Response.Redirect(Request.QueryString["ReturnUrl"].ToString());
}
else
{
this.Response.Redirect("Default.aspx");
}
Upvotes: 3