Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirect to Login Page

Status
Not open for further replies.

JoeZim

MIS
Sep 11, 2003
87
US
I've created a custom Login.aspx page which will check ID and password in a SQL database. I've been trying to setup my application to do the following:

If a user goes directly to another page, other than login.aspx, it will automatically direct the user back to login.aspx to properly login. I've mostly tried <authentication mode="Forms">, but am having trouble. Here is what I have in web.config:

Code:
   <authentication mode="Forms">
		<forms name="Form1" loginUrl="login.aspx" >
		</forms>
   </authentication>


    <authorization>
        <allow users="?"
    </authorization>

I've tried a few techniques from various posts, but it never seems to work.

When it comes to assigning <forms name="Form1" & the loginUrl="login.aspx" >, what exactly does the forms name need to be? Is this even the right approach?

Joe

 
I use a session value once the user logs in and pass that value around with the user. if the session expires and the user tries to do something they get redirected to the login page to relogin. this is simple and works for what I am currently doing.

 
Thanks for your quick reply dvannoy. Works well. I put a quick check for the session value at Page_Load and it redirects if the value is empty.

Thanks!
 
To use the framework that asp.net has for forms authentication, you have to do the following:

In web.config:
<authentication mode="Forms" >
<forms name=".CK001" loginUrl="login.aspx" protection="All" path="/" />
</authentication>

<authorization>
<deny users="?" />
<allow users="*" />
</authorization>

.CK001 is the name of the cookie that asp.net uses to know which user is authenticated or not.
You need to put deny users="?" and allow users="*" in that order to get the redirection to your page. This is all you need for the redirection to the login page.

In the loging page, you can do the following:
... in a Click event of the Login button

If Me.Validate() Then
Dim tkt As FormsAuthenticationTicket
Dim strValorCookie As String
Dim ck As HttpCookie

tkt = New FormsAuthenticationTicket(1, TxtUsuario.Text, DateTime.Now(), DateTime.Now().AddHours(1), False, "")

strValorCookie = FormsAuthentication.Encrypt(tkt)
ck = New HttpCookie(FormsAuthentication.FormsCookieName, _
strValorCookie)
Response.Cookies.Add(ck)
Response.Redirect("default.aspx")
End If

There is an easier approach with FormsAuthentication.RedirectFromLoginPage(), but I don't have the code at hand.

Jose Carlos



JOSE CARLOS ARAMBURU
josecarloaa@hotmail.com
 
Thanks Jose Carlos. I will give this a shot as well!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top