Solved

ASP.Net Login Control Redirect

Posted on 2011-03-21
6
1,116 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:dodgerfan
  • 4
  • 2
6 Comments
 
LVL 10

Expert Comment

by:khan_webguru
Comment Utility
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
 

Author Comment

by:dodgerfan
Comment Utility
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
 
LVL 10

Expert Comment

by:khan_webguru
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Accepted Solution

by:
khan_webguru earned 500 total points
Comment Utility
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
 
LVL 10

Expert Comment

by:khan_webguru
Comment Utility
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
 

Author Closing Comment

by:dodgerfan
Comment Utility
Thanks for the help and the links.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now