Asp.net authentication (simplest)

Source: Internet
Author: User

Generally, the authentication method of a website goes through the following steps:
1. Enter the user name and password, and click OK.
2. Check whether the user name and password are correct in the background. If the user name and password are correct, a prompt is returned. If the user name and password are correct, the accessible page is displayed.
In the ASP era, a session is usually created after the user name and password are verified to match, and then the session exists on each page to be verified. If yes, the page content is displayed; if it does not exist, generate a prompt and jump to the logon page.
However, in the Asp.net era, this process has been greatly reduced, and you do not need to verify the session on every page to be verified. You only need to perform the following steps, to complete the authentication process.
Step 1: Modify the Web. config file.
1. In the <system. web> and </system. in Web>, find the <authentication> section and change it to <Authentication mode = "forms"/>. forms indicates form authentication.
2. Add "<authorization> <deny users = "? "/> </Authorization>", where "<deny users = "? "/>" Indicates that all anonymous users are rejected.
Step 2: Create the login. aspx file.
After the first step, no matter which file the user accesses the website, Asp.net will automatically jump to login as long as it has not been authenticated. on the aspx webpage, and use the returnurl parameter in the URL to pass the webpage currently accessed by the user.
If the user directly accesses the test. aspx file without authentication, Asp.net automatically redirects to the login. aspx webpage. In this case, the URL in the address bar of the browser window is: "login. aspx? Returnurl = % 2ftest. aspx ". Therefore, you can jump the webpage back to the webpage specified by the returnurl parameter after the authentication is passed.
Step 3: Verify the identity in the login. aspx file.
The authentication method is relatively simple. Generally, a text box and a password box are created. After you enter the user name and password, click the submit button to authenticate the identity in the database, if the user name is 1 and the password is 2, the authentication is successful.
After Authentication, use formsauthentication. setauthcookie () to create an authentication ticket for the user and add it to the cookie. After accessing other web pages on the website, you do not need to use authentication. Click Code As shown below. Copy code The Code is as follows: protected void button#click (Object sender, eventargs E)
{
// Authentication method. In this example, the user name is 1 and the password is 2.
If (textbox1.text = "1" & textbox2.text = "2 ")
{
/*
* Create an authentication ticket for the user name and add it to the response cookie.
* The first parameter of setauthcookie is the name of the verified user.
* The second parameter of setauthcookie is true. The table creates a persistent cookie (a cookie that is stored in a cross-browser session). If it is false, the user needs to re-verify the identity after closing the browser.
*/
Formsauthentication. setauthcookie (textbox1.text, false );
}

// If the returnurl parameter is not passed in the URL, the page jumps to default. aspx; otherwise, the page jumps to the page specified by the returnurl parameter value.
If (string. isnullorempty (request. querystring ["returnurl"])
{
Response. Redirect ("default. aspx ");
}
Else
{
Response. Redirect (request. querystring ["returnurl"]. tostring ());
}
}

in just three steps, you can perform authentication. Is it cool?
This example is successfully tested in vs2005.
in this example, the process and code are very simple.
in this example, the website must be authenticated. You cannot specify which files can be accessed anonymously and which files cannot be accessed anonymously.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.