FormsAuthentication from non login page

Posted on 2006-05-07
Last Modified: 2006-11-18
I am trying to create a login system from a page other than login.aspx, as a matter of fact it is the home page itself.  I know that this must be an easy question but ive been working on it for a little while now to no avail.  I need to authenticate the user and redirect to a page.  Simple.  This is not the login.aspx page although there is this page available.

here is the code i am using to no avail, ive tried other things too but they didnt work either

        If Membership.ValidateUser(txtUserName.Text, txtPassword.Text) Then

            If FormsAuthentication.Authenticate(txtUserName.Text, txtPassword.Text) Then
            End If

        End If
Question by:rodmjay
    LVL 4

    Accepted Solution

    You are really close on this one.

    There is another method in the FormsAuthentication class that handles the redirect for you.  So instead of using response.redirect use FormsAuthentication.RedirectFromLoginPage.

    Like this in C#:

    void Login_Click(Object sender, EventArgs e) {
                if (FormsAuthentication.Authenticate(username.Text, password.Text))
                    FormsAuthentication.RedirectFromLoginPage(username.Text, true);
                    status.InnerHtml += "Invalid Login";

    The bool 'true', as the second parameter in the FormsAuthentication.RedirectFromLoginPage() is a value you (or your user) can set to persist the cookie beyond one session.  In other words it links up to that 'save my login information' checkbox you usually see below the login textboxes.

    Now, you also ask about creating a login on the home page and not on login.aspx.  You can create a login form anywhere.  It doesn't have to be called login.aspx.  In order to change the 'default' login page you can edit your web.config file as follows:

    <authentication mode="Forms">
          <forms loginUrl="<--SomeOtherPage.aspx-->" protection="All" timeout="30">

    So you would replace <--SomeOtherPage.aspx--> with your homepage filename.

    Good luck,
    LVL 8

    Author Comment

    Your code looks pretty good.  For my scenerio where i still want the login.aspx to be the default login page, i needed to add the following code to enable the home page to use the redirectfromloginpage in the web config

     <authentication mode="Forms">
          <forms loginUrl="login.aspx"
          defaultUrl="~/Institute/MyInstitute.aspx" />  <----- This is where the redirect will go if no request has been made
    LVL 4

    Expert Comment

    Great! Glad I helped a bit.  Glad you found an answer that solved the problem.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    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…
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 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

    14 Experts available now in Live!

    Get 1:1 Help Now