Sometime we need to create user(if new) or login(Authenticate) to view particular page.
and after user is logged in, we need to return him to the same page.
And if new account is created user should logged-in and then need to return him to the same page.
to achieve this we can user Request.UrlReferrer property and can redirect back to original URL.
On Page_Load event
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["ReferrerUrl"] = Request.UrlReferrer.ToString();
}
}
Function to return to same page.
private void ReturnBackExistingUser()
{
object referrer = ViewState["ReferrerUrl"];
if (referrer != null)
{
Response.Redirect(referrer.ToString());
}
else
{
Response.Redirect("Default.aspx");
}
}
If we have created new user then Add one line in function to authenticate new user.
FormsAuthentication.SetAuthCookie(username, false);
where username is newly created aspnet user.
so function to authenticate new user and then return him to the same page is:
and after user is logged in, we need to return him to the same page.
And if new account is created user should logged-in and then need to return him to the same page.
to achieve this we can user Request.UrlReferrer property and can redirect back to original URL.
On Page_Load event
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["ReferrerUrl"] = Request.UrlReferrer.ToString();
}
}
Function to return to same page.
private void ReturnBackExistingUser()
{
object referrer = ViewState["ReferrerUrl"];
if (referrer != null)
{
Response.Redirect(referrer.ToString());
}
else
{
Response.Redirect("Default.aspx");
}
}
If we have created new user then Add one line in function to authenticate new user.
FormsAuthentication.SetAuthCookie(username, false);
where username is newly created aspnet user.
so function to authenticate new user and then return him to the same page is:
private void ReturnBackNewUser()
{
FormsAuthentication.SetAuthCookie(username, false);
object referrer = ViewState["ReferrerUrl"];
if (referrer != null)
{
Response.Redirect(referrer.ToString());
}
else
{
Response.Redirect("Default.aspx");
}
}
{
FormsAuthentication.SetAuthCookie(username, false);
object referrer = ViewState["ReferrerUrl"];
if (referrer != null)
{
Response.Redirect(referrer.ToString());
}
else
{
Response.Redirect("Default.aspx");
}
}
Either Luv Coding or leave it...;-)
Comments
Post a Comment