Automatic Log In

What this tutorial does is instead of having multiple login pages for different things, you can just have one universal login page that automatically sends the values of the form to the requesting page that sets the value to a session variable..

Our Goal

What this tutorial does is instead of having multiple login pages for different things, you can just have one universal login page that automatically sends the values of the form to the requesting page. This is my first tutorial and although I have it as a server behavior, I don't know how to package it quiet well. If you know how to, please e-mail me at dloverinu@hotmail.com

Files

secured.asp — any page you need to protect
login.asp — page that secured.asp redirects to if session variable or username doesn't exist.
redirect.asp — page that will direct user after the initial log in.

What this does is checks to see if a session variable exist first and if it doesn't on that page, it redirects the page to the universal login page. the login page then logs into the redirect.asp, which in turn directs to the secured.asp. Bold words mean you can modify them to your need

pagetype = querystring that will tell redirect.asp which page to redirect to.

What we want to do in this tutorial is to check on secured.asp page if the session variable ("svUsername") exist and redirects if it doesn't exist.

Protected Pages

On any page you wish to protect (secured.asp), put after <%@LANGUAGE="VBSCRIPT"%>

<%
If Session("svUsername") = "" Then
  Response.Redirect("login.asp?pagetype=1")
End If
%>

The Login Page

On login.asp put this after <%@LANGUAGE="VBSCRIPT"%>

<%
Dim what
Dim pagetype
pagetype = Request.QueryString("pagetype")
what = "redirect.asp?pagetype=" & pagetype
%>

and set the form method to "post" and the action to <%=what%>

The Redirect Page

The redirect page is the page that will direct user after the initial log in.

On redirect.asp put this after <%@LANGUAGE="VBSCRIPT"%>

<%
Session("svUsername") = Request.Form("user_name")
Session("svPassword") = Request.Form("password")

Dim what
Dim pagetype
Dim page
pagetype = Request.QueryString("pagetype")
If pagetype = "" Then
  page = "secured.asp"
ElseIf pagetype = "1" Then
  page = "secured.asp"
ElseIf pagetype = "2" Then
  page = "../members/editProfile.asp" '<<<<<<<< any page you wish to secure
ElseIf pagetype = "3" Then
  page = "../pictures/editPictures.asp" '<<<<<<<< any page you wish to secure
ElseIf pagetype = "4" Then
  page = "../admin/default.asp" '<<<<<<<< any page you wish to secure
End If
Response.Redirect page
%>

The Secured Pages

Remember to put this on any page you wish to secure. All you need to do is change the pagetype number and modify the respective page value

<%
If Session("svUsername") = "" Then
  Response.Redirect("login.asp?pagetype=2")
End If
%>

Comments

I suppose...

August 3, 2002 by Steven Jones
You could do it like that, or you could include the login with every page, and then you wouldn't lose the querystring when logging in.

You must me logged in to write a comment.