ASP.net gets the session for simple user logon verification. __.net

Source: Internet
Author: User
The design contains a Web site containing two pages of Login.aspx and welcome.aspx. Require the user login interface as shown below, enter a valid username and password to open the Welcome.aspx page, this time the page displays the user name and welcome information. If the user level is admin, the "Manage all users" and "Modify personal Information" link buttons appear in the page, and if the user level is the normal page, only the Modify personal Information link button appears.
[Request]:
1. Legal username and password, set by yourself, at least three groups;
2. Enter the wrong username and password, the screen will pop-up message box, if the user tries to bypass the login page, direct access to welcome.aspx will pop-up prompt box;

3. Request to use Session object;


Development environment: VS2015

To create an empty Web site, create a new default.aspx,login.aspx,welcome.aspx Web page.

Analysis, add the appropriate code.

Login.aspx

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Login.aspx.cs" inherits= "Login"%>
<! DOCTYPE html>
Login.aspx.cs

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;

Using System.Web.UI.WebControls;
        Public partial class Login:System.Web.UI.Page {public struct MyUser {public string uname;
        public string pw;
    public int grade; } myuser[] MyUser = new myuser[3];//creates a newer struct protected void Page_Load (object sender, EventArgs e) {/
        /To assign values to the structure, their defined username, password and permissions myuser[0].uname = "Admin";
        MYUSER[0].PW = "admin";
        Myuser[0].grade = 1;
        Myuser[1].uname = "US2";
        MYUSER[1].PW = "US2";
        Myuser[1].grade = 0;
        Myuser[2].uname = "US3";
        MYUSER[2].PW = "US3";              

    Myuser[2].grade = 0;
        } protected void button1_click (object sender, EventArgs e) {int j = 0;
        Receives the username and password from the text box string name = TextBox1.Text;
        string pw = TextBox2.Text;     Iterate the user name and password that you entered to match your definition  
        for (int i = 0; i < 3; i++) {if (name = = Myuser[i].uname && pw = = MYUSER[I].PW) {
                session["UserName"] = name;
                Session["GR"] =myuser[i].grade;
            Response.Redirect ("welcome.aspx");
        } j + +;
        } if (j = = 3) {Response.Write ("<script>alert (' Username or password error ');</script>");      
 }
        
    }
}
Welcome.aspx.cs

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;

public partial class Welcome:System.Web.UI.Page
{
    protected void Page_Load (object sender, EventArgs e)
    { C9/>if (session["GR"]!= null)
        {
            if ((int) session["gr"] = = 1)
            {
                Response.Write (session[) UserName "+" Welcome to login <br> "+" <a href= ' # ' > Manage all Users </a><br><a href= ' # ' > Modify personal Information </a> ');
            }
            else
            {
                Response.Write (session["userName"] + "Welcome to login <br>" + "<a href= ' # ' > Modify personal Information </a>");
            }
        }
        else {
            Response.Write (' <script>alert (' Enter user name or password ');</script> ');}

The benefit of ASP.net is to separate the code that is displayed on the front end from the background logic code, which is the ASPX file and the Aspx.cs file. Of course, some of the code in Aspx.cs can also be used to display the front end.

The key point of this experiment

The use of 1.session.

2. Access the data to the session and take it out.

3. How to save your own set username and password, here use the structure of the array to save.

4.response.redirect is used for page jumps, Response.Write pop-up prompt boxes.





Related Article

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.