code example-Ask for Login

Source: Internet
Author: User
The need for a security system is obvious if your pages include sensitive information.

This sample shows you to setup some of your pages to ask for login if the user has not logged in yet.

2enetworx/dev site uses this kind the protection for interactive pages and file downloads.

Let ' s assume that we have a page where the visitors can ask questions. We want to make sure the user
has logged in before being able to ask a question.

If the user has not logged in yet, we'll ask for a login. After a successful login, we'll redirect the
User to the page requested.

We'll have a common file to check for login. You are need to include this file in all the pages
that require login.

Security.asp

<%

Bloggedin = (Len (Session ("UserName")) > 0)

If Brequirelogin Then
' Login required
If not Bloggedin then
' Not logged in, ask for login
Response.Redirect "Login.asp?comebackto=" & _
Request.ServerVariables ("Script_name") & "?" & _
Server.URLEncode (Request.QueryString)
' How do we construct the page to come back
End If
End If

%>

Login.asp

The the page where the user is redirected from thing. The variable
Sreferer is used to redirect the page of the user has come from.

Sgobackto variable'll used in the <form> tags for persisting this location when we submit the login form.

<%

If Request ("Comebackto") <> "then
Sreferer = Request ("Comebackto")
Sgobackto = "?" & Request.QueryString
End If

If Request ("Cmdlogin") <> "then

' Login Form submitted
sUserName = Request ("txtUserName")
Spassword = Request ("Txtpassword")

' Check for username and password

If sUserName = "Bill" and Spassword = "Gates" Then
Bloginsuccessful = True
End If

Session ("UserName") = sUserName


' After a successful login, let ' s send the user
' Back to the page requested. The variable Sreferer
' Holds the page to go back, if it is empty, we should
' Redirect the user to our default page.

If Sreferer = "" Then
Response.Redirect "Index.asp"
Else
Response.Redirect Sreferer
End If

Else

' Display the Login Form '

%>

<form action= "login.asp<%=sgobackto%>" method= "POST" >
<input type= "text" name= "txtUserName" ><br>
<input type= "Password" name= "Txtpassword" ><br>
<input type= "Submit" Name= "Cmdlogin" ><br>
</form>

<%

End If

%>

Testpage.asp

Now this is the security.asp file of the need to be setting Brequirelogin to True and including.

<%

Brequirelogin = True

%>

<!--#include file= "security.asp"-->

The Users should be logged into to the this text.

I hope this sample gives your a quick overview of a simple security system implementation with ASP.

are always welcome to express your feedback!

by Hakan Eskici



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.