如何使非常简单的ASP.Net密码保护页面

时间:2011-01-06 20:37:21

标签: asp.net authentication

我正在寻找一个非常简单的密码保护ASP.Net页面的解决方案。

我找到了我正在寻找的here,但它在ASP中,我不知道将其转换为ASP.Net的语法。

它只是创建一个临时cookie,一旦关闭浏览器窗口就会过期。

我不打算将用户名/密码存储在数据库中。我偶尔会手动更改密码。

简单地帮我将以下代码转换为ASP.Net就太棒了!

这将在logon.aspx页面上进行,并从表单中提取值。

Username="Administrator"
Password="Admin"
Validated = "OK"

if Strcomp(Request.Form("User"),Username,1)=0 AND Request.Form("password") = Password then
    Response.Cookies("ValidUser") = Validated

    If (Request.QueryString("from")<>"") then
        Response.Redirect Request.QueryString("from")
    else
      Response.Redirect "MyPage.aspx"
    End if    
Else
    If Request.Form("User") <> "" then
        Response.Write "<h3>Authorization Failed.</h3>" & "<br>" & _ "Please try again.<br>&#xa0;<br>"
    End if
End if

这会在受密码保护的页面上进行,以确认已创建cookie。

Validated = "OK"

if Request.Cookies("ValidUser") <> Validated then
dim s
s = "http://"
s = s & Request.ServerVariables("HTTP_HOST")
s = s & Request.ServerVariables("URL")

if Request.QueryString.Count > 0 THEN
    s = s & "?" & Request.QueryString 
end if

Response.Redirect "Logon.aspx"
End if

1 个答案:

答案 0 :(得分:5)

只需使用内置表单身份验证并在web.config中设置凭据存储。

Here's a quick and dirty example

Another example