ASP.Net Login Control Redirect

I have a asp.net login control for my site. When a user logs in the page does not redirect anywhere, it just sits at the login page. It's logging you in, as it show your name up top. But I cannot get it to redirect to another page after successful login. How do I get it to redirect tp a certain page? I'm using ASP.Net 4.0.
dodgerfanAsked:
Who is Participating?
 
khan_webguruConnect With a Mentor Commented:
Hello Bro,

I will try to explain how to use Login control in ASP.Net applications. In The discussion below you will not dig deep into advanced usage of this control or customization of this control. I will discuss simple usage. I will be talking about advanced settings and usage in other articles.

Login control can use either MembershipProvider specified in web.config file, or can explicitly assign Membership provide or you can do custom authenication. To start with basics lets assume that you already have authentication methods implemented using your existing database and you want to leverage that infrastructure with Login control. So what you need to do is, add an handler for OnAuthenticate event handler for your control and authenticate user via that method.


If authentication fails user stays on the same login page. On successful login the user gets redirected to appropriate page. There are couple of ways of specifying the page where user gets sent after successful authentication.


A query string parameter ReturnUrl with a value of URL for redirection is one way. After authentication the implemnetation of Login control will look at this query string parameter and redirect the user to that URL.

You can set DestinationPageurl property of the control and set the URL of target page in there.

You can handle OnLoggedin event of the control and manually call Response.Redirect to targer page.


Following code snippets show what the attributes on page look like and how OnAuthenticate event handler sets Authenticated value in the event argument.

 
<asp:login id="ctlLogin" runat="server" onauthenticate="OnAuthenticate"
    onloggedin="OnLoggedIn" onloggingin="OnLoggingIn" destinationpageurl="~">
</asp:login>

protected void OnAuthenticate(object sender, AuthenticateEventArgs e)
{
    e.Authenticated = false;
    if (String.Compare(ctlLogin.UserName, "foo", true) == 0 &&
        String.Compare(ctlLogin.Password, "bar", false) == 0)
    {
        e.Authenticated = true;
    }
}		

protected void OnLoggedIn(object sender, EventArgs e)
{
    Response.Redirect("~");
}

Open in new window


Hope this will help you in your problem.

Regards,

Asif Ahmed Khan
0
 
khan_webguruCommented:
Typically, when entering a site, the user starts out at the Default.aspx page. Many times there is a login control on that page or they click on a link that takes them to a login page. I'm can only assume that yours is set up similarly. In that scenario, everything should work fine and your web.config should do what it is supposed to do.

If however, your user is trying to access a page that is restricted (requires logging in), they are automatically redirected to the login page and you will see the "ReturnUrl=" in the URL (Querystring) of the login page. If that is the case, then that will override whatever you have in the web.config so that when they do login successfully they will be sent back to the page that automatically sent them to the login page and not the page you specified in the Web.config. Make sure you don't have your users trying to access a restricted page before they login. Give them a way to log in before they attempt to access a restricted page.

Best way to test this is to set your login page as your default start page by right-clicking on the login page in the solution explorer and selecting "Set as Start Page". Then run the site, log in and see if you get redirected to PatientInformation.aspx.  If everything works like you want, then you need to modify the way the user comes into your site as I outlined above. If you still have trouble, I can probably be of more help if you let me know how you have your users getting to that login control, is it on it's own page and is the PatientInformation.aspx in the root folder of the website? You probably already know this but if your calling ~/PatientInformation.aspx using the "~/", you need to make sure that page is in the root folder of the website. Let me know if that helps. From what it sounds like, your code is correct, it's just the way the user is coming into the site that needs to be fine-tuned.


Any way please for more help browse the following links

http://forums.asp.net/t/1403132.aspx/1?Login+control+FAQ+

http://www.aspnettutorials.com/tutorials/controls/asp4-login-contrls.aspx

Hope this will solve your problem.

Regards,

Asif Ahmed Khan
0
 
dodgerfanAuthor Commented:
Thanks for the information. It has helped out some. My web.config had the wrong url for login.aspx. NOw when I log in i goes to the default page. That's a little different from what I was trying to do. When users frist open up the app, it's at the default.aspx page, which is open for any user. They can then click the login link, which takes them to the login.aspx page. Once they click the login button, I want to redirect to the Main.aspx page, not back to the default.aspx page, which it is not doing. All of these pages are in the root folder thus far. Id there a way to do this? Thanks for the help.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
khan_webguruCommented:
I will try to explain how to use Login control in ASP.Net applications. In The discussion below you will not dig deep into advanced usage of this control or customization of this control. I will discuss simple usage. I will be talking about advanced settings and usage in other articles.

Login control can use either MembershipProvider specified in web.config file, or can explicitly assign Membership provide or you can do custom authenication. To start with basics lets assume that you already have authentication methods implemented using your existing database and you want to leverage that infrastructure with Login control. So what you need to do is, add an handler for OnAuthenticate event handler for your control and authenticate user via that method.


If authentication fails user stays on the same login page. On successful login the user gets redirected to appropriate page. There are couple of ways of specifying the page where user gets sent after successful authentication.


A query string parameter ReturnUrl with a value of URL for redirection is one way. After authentication the implemnetation of Login control will look at this query string parameter and redirect the user to that URL.
0
 
khan_webguruCommented:
Hello Bro!

I found some more useful link please have a look might be possible something will help yo fix your problem.

http://forums.asp.net/t/1248381.aspx

http://weblogs.asp.net/scottgu/archive/2005/10/18/427754.aspx

http://blog.waltritscher.com/index.php/2006/05/18/tip-aspnet-20-login-control-not-redirecting/

Regards,

Asif Ahmed Khan
0
 
dodgerfanAuthor Commented:
Thanks for the help and the links.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.