简体   繁体   English

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

[英]Redirect user to the appropriate page from ASP classic login page

I have a website on which certain pages are secured with a login page written in vbscript (framework is asp classic). 我有一个网站,其中使用vbscript编写的登录页面来保护某些页面(框架是asp经典)。 Given that the username is "foo" and the password is "bar", the login page currently accepts the username and password and then redirects the user to a default page. 鉴于用户名为“foo”且密码为“bar”,登录页面当前接受用户名和密码,然后将用户重定向到默认页面。 We will call it "page1". 我们称之为“page1”。 Below is the code: 以下是代码:

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

This works but the user will always be redirected to the same page, regardless of which page they were trying to reach. 这样可行但用户将始终被重定向到同一页面,无论他们尝试访问哪个页面。 I would like the user to be redirected to the page they were trying to reach before being sent tho the login page. 我希望用户在被发送到登录页面之前被重定向到他们尝试访问的页面。 Supposing the user wanted to go to "page2", I tried the following code: 假设用户想要转到“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

This does not work, which is probably because 这不起作用,这可能是因为

Request.ServerVariables("URL")

is pulling the url from the login page. 正在从登录页面中提取网址。 Does anyone know how to send the user to the page that was originally requested? 有谁知道如何将用户发送到最初请求的页面?

Thanks in advance for any help/advice! 在此先感谢您的任何帮助/建议!

On your login form page add a hidden field for the referring page and populate the value like this: 在您的登录表单页面上为引用页面添加一个隐藏字段,并填充值如下:

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

Then redirect to this page once the form is submitted successfully: 成功提交表单后重定向到此页面:

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

I don't know about "classic asp", nor VB script. 我不知道“经典asp”,也不知道VB脚本。

But what about the approach of adding the desired page (requiring login) in a querystring ? 但是在查询字符串中添加所需页面(需要登录)的方法呢?

forcing to land the user on login.aspx?redirectUrl=desiredPage.asp 强制登陆用户登录login.aspx?redirectUrl = desiredPage.asp

then when login is done, you redirect to the page by retrieving it from the querystring ? 登录完成后,通过从查询字符串中检索页面重定向到页面?

This page explains it better. 这个页面更好地解释了它。

ASP.NET: directing user to login page, after login send user back to page requested originally? ASP.NET:指导用户登录页面,登录后发送用户回到原来请求的页面?

Every secured page should have some header code like this: 每个安全页面都应该有一些标题代码,如下所示:

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

I typically put this into an include file called "private.asp" and make sure to include it at the top of every page that should be secured. 我通常将其放入名为“private.asp”的包含文件中,并确保将其包含在应保护的每个页面的顶部。

In your login page, after you've successfully logged in the user, check your querystring value to see if you should forward the user back to an originally requested page: 在您的登录页面中,在您成功登录用户后,检查您的查询字符串值以查看是否应将用户转发回最初请求的页面:

' 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

your signin page URL should look like that: 您的登录页面网址应如下所示:

http://domain.com/login.asp?urlstr=page2.asp 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM