Getting started
Authentication is often used in website creation. Asp.net has several built-in authentication methods, such as Windows, Froms, and Passport. These authentication methods are different. 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 page 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. The code after clicking the submit button is as follows.
Protected void button#click (object sender, EventArgs e) {// authentication method. In this example, the user name is 1, 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-authentication after the browser is closed */FormsAuthentication. setAuthCookie (TextBox1.Text, false);} // jump to Default if the ReturnUrl parameter is not passed in the URL. aspx, otherwise jump 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 ());}}