asp.net - 如何制作非常简单的 ASP.Net 密码保护页面

标签 asp.net authentication

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

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

它只是创建一个临时 cookie,一旦用户关闭浏览器窗口,该 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

最佳答案

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

Here's a quick and dirty example

Another example

关于asp.net - 如何制作非常简单的 ASP.Net 密码保护页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4619579/

相关文章:

asp.net - 在 ASP.NET 中实现可编辑的 DropDownList

c# - 将空值绑定(bind)到 Web 用户控件的属性

mysql - 如何在 GridView 中添加待处理列

c# - 什么是应用程序变量?我如何在 ASP.NET MVC 中声明应用程序变量?

php - 我的登录无法正常工作,我不明白为什么? [请帮忙]

cakephp-1.3 - cakePHP Auth - 允许/拒绝, isAuthorized 实际需要什么?

asp.net - 有时我的文本框没有样式

Angular 8 在身份验证完成之前抑制 HTTP 调用

api - Jawbone UP API oAuth 和访问 token

javascript - Facebook 登录窗口在未登录应用程序的情况下出现和消失