Binding the value of a session to a page in MVC

Source: Internet
Author: User

The first step is to create a stored procedure in the SQL Server database that queries the role of the user name (employee name):

 if  exists (select  * from  sys.objects "  Span style= "color: #800000;" >proc_select_login   " ) begin Drop Procedure Proc_select_loginendgocreate procedure proc_select_login @ename nvarchar ( 20 ),-- 20 )- - password  as   Select  j.jname from  employinfo,jobinfo J  [email protected] and Epwd= @epwdgo  

The second step is to create the Selectlogin method under the business logic EmployeeDAO class in model, use the ExecuteScalar () method to query the role of the user name (employee name), and return the string type:

 /// <summary>        ///Administrator Login/// </summary>        /// <param name= "ename" ></param>        /// <param name= "epwd" ></param>        /// <returns></returns>         Public stringSelectlogin (stringENAME,stringepwd) {            stringsql ="Proc_select_login"; SqlCommand cmd=NewSqlCommand (sql, con); Cmd.commandtype=CommandType.StoredProcedure; Cmd. Parameters.addwithvalue ("@ename", ename); Cmd. Parameters.addwithvalue ("@epwd", EPWD); Con.            Open (); stringJname =string.            Empty; Jname=convert.tostring (cmd.            ExecuteScalar ()); Con.            Close (); returnJname; }

In the third step, create a. cs file with the name Employeecontroller in the controller controllers, and call the EmployeeDAO class under the Selectlogin () method for employee login verification. Log in successfully, page jump, and the user name (employee name) and its role stored in the session,

Note: String Txtcheck is a verification code that accepts writes on the page and must match the name in the CAPTCHA input tag on the page.

/// <summary>    ///Employee Login Verification/// </summary>    /// <param name= "collection" ></param>    /// <param name= "Txtcheck" >Verification Code</param>    /// <returns></returns>[HttpPost] PublicActionResult Login (FormCollection collection,stringTxtcheck) {                   //receive the user name and password entered on the pageEmploy. ename = collection["txtname"]; Employ. Epwd= collection["txtpwd"]; stringstr = convert.tostring ( This. tempdata["Code"]). ToLower ();//receive the verification code in the temporary data        stringJname =DAO. Selectlogin (employ. ENAME, employ.        EPWD); if(jname!=string. Empty) {if(str! =Txtcheck.tolower ()) {viewbag.errormsg="Verification Code Error"; returnView (employ); }            Else            {                //use session to store                 This. session["ename"] =employ.                ename; //use session to store                 This. session["Jname"] =Jname; //This . Session.add ("employ", employ);                returnRedirecttoaction ("Index","Home"); }        }        Else{viewbag.errormsg="incorrect user name or password"; returnView (employ); }            }}

The fourth step, create the login page, I embed here is the HTML code:

<body> <form action="/employee/login"Method="Post"> <table border="0"align="Center"cellpadding="Ten"cellspacing="5"style="margin-top:150px;"> <tr> <td width="31%"height=" *" class="login-text02">User name:<br/> </td> <td width="69%"> <input type="text"Name="txtname"Id="txtname"@*value="@Model. ename"*@/> </td> </tr> <tr> <td height=" *" class="login-text02">Password:<br/> </td> <td> <input type="Password"Name="txtpwd"Id="txtpwd"@*value="@Model. Epwd"*@/> </td> </tr> <tr> <td height=" *" class="login-text02">Verification Code:<br/> </td> <td> <div style="width:400px;"> <input type="text"Name="Txtcheck"/>&nbsp;&nbsp;&nbsp; "Valicode"style="Cursor:pointer;"Src="/validatecodeimg/get"title="can't see clearly, click to change a picture"alt="can't see? Please point me"onclick="Imgcodes (this);"/> &nbsp;&nbsp; <a href="javascript:imgcodes ();"> can not see Clearly, click here </a> </div> </td> </tr> <tr> &LT;TD height=" *"> &nbsp; </td> <td> <input type="Submit"Id="Btnlogin"Value="Login" class="btn btn-success"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="Reset"Value=" Reset" class="btn btn-warning"/> <label style="color:red;"> @ViewBag .errormsg</label> </td> </tr> </table> </form></body& Gt

In the fifth step, the controller is created to be named HomeController, accepting the users and their roles stored in the previous session:

 Public actionresult Top (formcollection Collecton)        {                   string ename = convert.tostring (this. session["ename"]); // receive the verification code            in the session string this. session["jname"]. ToString ();             return View ();        }

The sixth step is to create a page for HomeController, binding the Stored User name and its role stored in the session:

<table width="100%"Border="0"cellspacing="0"cellpadding="0"> <tr> <td> "@Url. Content ("~/content/images/uesr.gif")"Width=" -"height=" -"> <spanclass="STYLE2">currently logged on User: @if (session["ename"] !=NULL)                                {                                        <span> @Session ["ename"]. ToString () </span>                                                                                              }&nbsp;&nbsp;&nbsp;&nbsp; Role: @if (session["Jname"]!=NULL)                    {                        <span> @Session ["Jname"]. ToString () </span>                    }            </span> </td> </tr></table>

Show Results:

Binding the value of a session to a page in MVC

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.