Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Make Very Simple ASP.Net Password Protected Page

I am looking for a very simple solution to password protect an ASP.Net page.

I found exactly what I am looking for here but it is in ASP and I do not know the syntax to convert it to ASP.Net.

It simply creates a temporary cookie that will expire as soon as they close their browser window.

I am not looking to store the username / password in a db. I will manually change the password occasionally.

Simply helping me convert the following code to ASP.Net would be wonderful!

This goes on the logon.aspx page and pulls values from a form.

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

This goes on the password protected page to confirm the cookie was created.

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
like image 798
Joe Avatar asked Jan 06 '11 20:01

Joe


1 Answers

Just use the built-in forms authentication and setup your credentials store in the web.config.

Here's a quick and dirty example

Another example

like image 180
Didaxis Avatar answered Nov 05 '22 05:11

Didaxis