locked
how to redirect to particular page after login? RRS feed

  • Question

  • User-526823404 posted

    hi,

    any body knows how to redirect to particular page in the url after login in C# ?

    Example

    1) the home page is www.google.com/login.aspx it is login protected

    2) in this application i have another page(post.aspx)  www.google.com/post.aspx?a=25&b=35;

    the user try to enter the 2nd line of the link,  if the session is not valid it will automatically redirect to the first link, after enter the login id and pwd it should go directly to to the 2nd line of the link how?

    expect the answer from masters.........

     

     

    Friday, October 10, 2008 10:39 AM

Answers

  • User-2008565010 posted

    So if you want to use LoginControl with your own database ... you can use Authenticate Event of login control like this:

    http://www.netomatix.com/Development/LoginControlBasic.aspx

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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 10, 2008 1:12 PM
  • User-1763611275 posted

    Hi

    try this in login page

        protected void Login1_LoginError(object sender, EventArgs e)
        {
            //There was a problem logging in the user

            //See if this user exists in the database
            MembershipUser userInfo = Membership.GetUser(Login1.UserName);

            if (userInfo == null)
            {
                //The user entered an invalid username...

                Login1.FailureText = "There is no user in the database with the username " + Login1.UserName;
            }
            else
            {
                //See if the user is locked out or not approved
                if (!userInfo.IsApproved)
                {

                    Login1.FailureText = "Your account has not yet been approved by the site's administrators. Please try again later...";
                }
                else if (userInfo.IsLockedOut)
                {
                    Login1.FailureText = "Your account has been locked out because of a maximum number of incorrect login attempts. You will NOT be able to login until you contact a site administrator and have your account unlocked.";
                }
                else
                {
                    //The password was incorrect (don't show anything, the Login control already describes the problem)
                    Login1.FailureText = string.Empty;
                }
            }

        }


        protected void Login1_LoggedIn(object sender, EventArgs e)
        {
            TextBox TextBox1 = (TextBox)Login1.FindControl("UserName");
            //MembershipUser user = Membership.GetUser(TextBox1.Text);
            MembershipUser user = Membership.GetUser(Login1.UserName);

            if (Request.QueryString["ReturnUrl"] != null)
            {
                Login1.DestinationPageUrl=Request.QueryString["ReturnUrl"].ToString();
            }
            else
            {

                //-- check if login user in Admin role
                if (Roles.IsUserInRole(TextBox1.Text, "Admin"))
                {
                    Login1.DestinationPageUrl="~/Admin/Default.aspx";

                }
                //-- check if login user in User role
                else if (Roles.IsUserInRole(TextBox1.Text, "User"))
                {
                    Login1.DestinationPageUrl="~/User/Default.aspx";
                }
            }
           
        }

    Good Luck

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 10, 2008 5:46 PM

All replies

  • User307176038 posted

    on ur login.aspx.cs after checking the right username and password you have to write code like,

    FormsAuthentication.RedirectFromLoginPage(userName.Text, true/false);

    this is the perfect way of doing the same, if you have any confusion revert back

    Friday, October 10, 2008 12:08 PM
  • User-2008565010 posted

    when using Forms Authentication .. you can make use of ReturnUrl

    string returnUrl = Request.QueryString["ReturnUrl"];

    // check if it exists, if not then redirect to default page

    if (returnUrl == null) returnUrl = "~/Default.aspx";

    Response.Redirect(returnUrl);

    or check this: http://geekswithblogs.net/ranganh/archive/2005/04/25/37612.aspx

    Friday, October 10, 2008 12:11 PM
  • User-526823404 posted

    i saw the article( http://www.aspnettutorials.com/tutorials/validation/authentication-aspnet2-csharp.aspx) posted regarding this.

    Should i add the below code in web.config? I dont want to add the user and password in web.config. Is there any other way to do this? Because i am using middle tier server for authentication for security reason.

    <authentication mode="Forms">
    <forms name=".SecurityDemo" loginUrl="LoginVerifyCsharp.aspx">
    <credentials passwordFormat="Clear">
    <user name="John" password="Foo"/>
    </credentials>
    </forms>
    </authentication>
    <machineKey validationKey="AutoGenerate" decryptionKey="AutoGenerate"/>
    <authorization>
    <deny users="?"/>
    </authorization>

    Friday, October 10, 2008 12:56 PM
  • User-2008565010 posted

    it is not necessary to store username password in web.config...

    in example you used above - "FormsAuthentication.Authenticate(tbName.Text, tbPass.Text))" method is used for authentication....

    you can use your own mechanism to Authenticate ...and decide if credentials entered are valid or not...

    are you using built-in asp.net Login Control?

    Friday, October 10, 2008 1:00 PM
  • User-526823404 posted

    yes

    Friday, October 10, 2008 1:10 PM
  • User-2008565010 posted

    So if you want to use LoginControl with your own database ... you can use Authenticate Event of login control like this:

    http://www.netomatix.com/Development/LoginControlBasic.aspx

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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 10, 2008 1:12 PM
  • User-1763611275 posted

    Hi

    try this in login page

        protected void Login1_LoginError(object sender, EventArgs e)
        {
            //There was a problem logging in the user

            //See if this user exists in the database
            MembershipUser userInfo = Membership.GetUser(Login1.UserName);

            if (userInfo == null)
            {
                //The user entered an invalid username...

                Login1.FailureText = "There is no user in the database with the username " + Login1.UserName;
            }
            else
            {
                //See if the user is locked out or not approved
                if (!userInfo.IsApproved)
                {

                    Login1.FailureText = "Your account has not yet been approved by the site's administrators. Please try again later...";
                }
                else if (userInfo.IsLockedOut)
                {
                    Login1.FailureText = "Your account has been locked out because of a maximum number of incorrect login attempts. You will NOT be able to login until you contact a site administrator and have your account unlocked.";
                }
                else
                {
                    //The password was incorrect (don't show anything, the Login control already describes the problem)
                    Login1.FailureText = string.Empty;
                }
            }

        }


        protected void Login1_LoggedIn(object sender, EventArgs e)
        {
            TextBox TextBox1 = (TextBox)Login1.FindControl("UserName");
            //MembershipUser user = Membership.GetUser(TextBox1.Text);
            MembershipUser user = Membership.GetUser(Login1.UserName);

            if (Request.QueryString["ReturnUrl"] != null)
            {
                Login1.DestinationPageUrl=Request.QueryString["ReturnUrl"].ToString();
            }
            else
            {

                //-- check if login user in Admin role
                if (Roles.IsUserInRole(TextBox1.Text, "Admin"))
                {
                    Login1.DestinationPageUrl="~/Admin/Default.aspx";

                }
                //-- check if login user in User role
                else if (Roles.IsUserInRole(TextBox1.Text, "User"))
                {
                    Login1.DestinationPageUrl="~/User/Default.aspx";
                }
            }
           
        }

    Good Luck

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 10, 2008 5:46 PM
  • User-642698451 posted

    I wanted to know how this events Login1_LoginError and Login1_LoggedIn are wired to the Login1.aspx?

    Sunday, November 28, 2010 7:17 AM
  • User-2008565010 posted

    e.g. <asp:Login ID="Login1" runat="server" OnLoggedIn="Login1_LoggedIn" OnLoginError="Login1_LoginError"
                OnLoggingIn="Login1_LoggingIn">

    note: If you are using VB.NET you might need a little different way.

    Or you can do it by double clicking the event in the properties window of login control.

    Login Events

    Monday, November 29, 2010 12:06 PM