• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

FormsAuthentication from non login page

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
                Response.Redirect("~/Institute/MyInstitute.aspx")
            End If

        End If
0
rodmjay
Asked:
rodmjay
  • 2
1 Solution
 
dtryonCommented:
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);
            else
                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">
      </forms>
    </authentication>

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

Good luck,
Davin
0
 
rodmjayAuthor Commented:
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
    </authentication>
0
 
dtryonCommented:
Great! Glad I helped a bit.  Glad you found an answer that solved the problem.

Davin
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now