Reputation: 54193
Say I am in the masterpage load event. If the user is logged in, I need to redirect them away from Login.aspx, if not logged in, I need to redirect them to Login.aspx. But first, I need to know which page they were trying to access.
How can I get this information?
Thanks
Upvotes: 0
Views: 841
Reputation: 86
So you could do is create a base class for all pages and in that class override the OnInit (EventArgs e) method. In this method, put the code that checks whether the user is signed on the system (by checking the session variable representing the user is not null) and if that variable is null then do the Redirect to the login page. If you follow this approach, all your content pages (except the login page) must inherit from this base class.
I hope this helps you.
public class BasePage : Page
{
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnInit(EventArgs e)
{
if (Session["Usuario"] == null)
{
Response.Redirect(ResolveClientUrl("~/Login.aspx"));
}
}
}
Upvotes: 1
Reputation: 101
This is a little confusing. You want the same Masterpage to redirect away from a Content Page (ex default.aspx) to a Login Page (ex Login.aspx) & from the Login Page to a Content Page. You only need to do one of these things.
First of all, you are going to require 2 Master Pages (or just remove the master page from Login.aspx) as you will get caught in recursive looping.
Say the user tries to access your site, and they aren't logged in. They will be redirected to Login.aspx, which will check and see that they aren't logged in, redirecting them to Login.aspx, and so on...
There should be no need to redirect from Login.aspx if they are already logged in, as they shouldn't come to this page in the first place unless they aren't.
Finally, in the Master Page perform a quick check to see if the user is logged in, if not Redirect to the Login Page and pass in the page they were requesting as a parameter of the url (As mentioned before, creating a Session variable works too).
If (!userLoggedIn)
{
Response.Redirect("Login.aspx?Url=" + Request.Url);
}
This passes in the Url they originally went to as a parameter (ex. Login.aspx?Url=default.aspx)
then in Login.aspx
If (userSuccessfullyLoggedIn)
{
Response.Redirect(Request.Params["Url"]);
}
This will redirect them to the Page that they originally requested (ex. default.aspx)
Upvotes: 0
Reputation: 103428
You could use Request.RawUrl
to get the current URL being requested.
if (Request.RawUrl.StartsWith("/login.aspx")){
//Check whether user is logged in
}
Upvotes: 0