从ASP经典登录页面将用户重定向到相应的页面

时间:2014-04-01 21:22:13

标签: vbscript asp-classic

我有一个网站,其中使用vbscript编写的登录页面保护某些页面(框架是asp经典)。鉴于用户名为“foo”且密码为“bar”,登录页面当前接受用户名和密码,然后将用户重定向到默认页面。我们称之为“page1”。以下是代码:

Response.Buffer = True
If lcase(Request.Form("username")) = "foo" AND lcase(Request.Form("password")) = "bar" then
Session.Contents("foo") = "1"
Response.Redirect("page1.asp")
Else
Response.Redirect("failure.asp")
End If

这样可行,但无论用户尝试访问哪个页面,用户都将被重定向到同一页面。我希望用户在被发送到登录页面之前被重定向到他们尝试访问的页面。假设用户想要转到“page2”,我尝试了以下代码:

Response.Buffer = True
If Request.ServerVariables("URL")= "http://www.mysite.com.com/page2.asp" AND lcase(Request.Form("username")) = "foo" AND lcase(Request.Form("password")) = "bar" then
Session.Contents("Dealer") = "1"
Response.Redirect("page2.asp")
ElseIf lcase(Request.Form("username")) = "foo" AND lcase(Request.Form("password")) = "bar" then
Session.Contents("foo") = "1"
Response.Redirect("page1.asp")
Else
Response.Redirect("failure.asp")
End If

这不起作用,这可能是因为

Request.ServerVariables("URL")

从登录页面中提取网址。有谁知道如何将用户发送到最初请求的页面?

提前感谢您的任何帮助/建议!

4 个答案:

答案 0 :(得分:2)

在您的登录表单页面上为引用页面添加一个隐藏字段,并填充如下所示的值:

<input name="referer" type="hidden" value="<%= Request.ServerVariables("HTTP_REFERER") %> />

然后在成功提交表单后重定向到此页面:

Response.Redirect(Request.Form("referer"))

答案 1 :(得分:1)

我不知道“经典的asp”,也不知道VB脚本。

但是在查询字符串中添加所需页面(需要登录)的方法呢?

强制登陆用户登录login.aspx?redirectUrl = desiredPage.asp

然后在登录完成后,您通过从查询字符串中检索它来重定向到该页面?

此页面更好地解释了它。

ASP.NET: directing user to login page, after login send user back to page requested originally?

答案 2 :(得分:1)

每个受保护的页面都应该有一些标题代码:

If Not Session("LoggedIn") Then
    Response.Redirect "login.asp?r=" & Server.UrlEncode(Request.ServerVariables("SCRIPT_NAME"))
End If

我通常把它放到名为&#34; private.asp&#34;的包含文件中。并确保将其包含在应保护的每个页面的顶部。

在您的登录页面中,在您成功登录用户后,请检查您的查询字符串值,看看您是否应该将用户转发回最初请求的页面:

' After successful login...
strReturnURL = Request.QueryString("r")

If Len(strReturnURL) > 0 Then
    Response.Redirect strReturnURL
Else
    ' Send them to your homepage...
    Response.Redirect "/"
End If

答案 3 :(得分:0)

您的登录页面网址应如下所示:

http://domain.com/login.asp?urlstr=page2.asp

Response.Buffer = True
dim redirecturl
 redirecturl = Request("urlstr")
If  lcase(Request.Form("username")) = "foo" AND lcase(Request.Form("password")) = "bar"  and len(redirecturl)>0 then
Session("Dealer") = "1"
Response.Redirect(redirecturl)
ElseIf lcase(Request.Form("username")) = "foo" AND lcase(Request.Form("password")) = "bar" and len(redirecturl)=0  then
Session("foo") = "1"
Response.Redirect("login.asp")
Else
Response.Redirect("failure.asp")
End If