C # uses the MVC framework for login verification

Source: Internet
Author: User
Tags connectionstrings

Step One: Demand analysis

My goal is to use the MVC framework for simple login verification. Enter the user name and password from the client. It is then passed to database validation. If the database exists with this user name ID and password, the success prompt for the client account name is returned. Otherwise, the client failure message is returned.

Step two: Build an MVC framework

New Project >web>web Application

Step three: First add a controller, then the Views folder to find the corresponding and Controller folder with the same name to build the view interface style, and finally modify the route to be able to access the browser

Controller:

public class Logincontroller:controller
{
Get:login
Public ActionResult Index ()
{
Return View ("LoginView");
}

VIEW:

<! DOCTYPE html>

<meta name= "viewport" content= "Width=device-width"/>
<title> Landing Interface </title>
<body>
<form method= "POST" action= "/login/login" > //action Submit and return to controller processing at this point you can first use # instead
User name: <input name= "LoginId" type= "text"/> <br/>
Password: <input name= "loginpwd" type= "password"/> <br/>
<input type= "Submit" value= "Login"/><br/>
@ViewData ["Info"] //browser Gets the contents of the controller
</form>
</body>

Routing RouteConfig.cs

Namespace Mvclogin
{
public class Routeconfig
{
public static void RegisterRoutes (RouteCollection routes)
{
Routes. Ignoreroute ("{resource}.axd/{*pathinfo}");

Routes. MapRoute (
Name: "Default",
URL: "{controller}/{action}/{id}",
defaults:new {controller = "Login", action = "Index", id = urlparameter.optional}
);
}
}
}

Step four: Browser access no problem, formally began to write content, first write the Models model section. Typically models contains entity classes, universal data access classes, Universal Database access classes (ADO)

1. The entity class is written. Write the user Login entity class. Corresponds to a database field.

Namespace Mvclogin.models
{
<summary>
Login entity class, properties: Login ID, login password, user name
</summary>
public class SysAdmin
{
public int LoginId {get; set;}
Public String loginpwd {get; set;}
Public String adminname{get; set;}

}
}

2. Universal database access class, where only the method of writing queries

Namespace Mvclogin.models
{
<summary>
Generic Data access Classes
</summary>
public class SQLHelper
{
//Define database connection string
public static string connstring = configurationmanager.connectionstrings["ConnString"]. ToString ();//Writes the database connection string in Web. config, which is called through the connectionstrings property of the ConfigurationManager class

public static SqlDataReader Getreader (String sql)
{
SqlConnection conn = new SqlConnection (connstring);
SqlCommand cmd = new SqlCommand (SQL, conn);
Try
{
Conn. Open ();
return CMD. ExecuteReader (commandbehavior.closeconnection);
}
catch (Exception ex)
{

Throw ex;
}
}
}

Web. config plus node

<connectionStrings>
<add name= "connstring" connectionstring= "server=.; Database=smdbweb; Uid=sa; Pwd=abc.123 "/>
</connectionStrings>

3. Write Entity access classes. The name of the entity Access class is generally the entity class name +service. Concrete action entities, encapsulating entities, returning entity objects

Namespace Mvclogin.models
{
<summary>
Data access Classes
</summary>
public class Sysadminservice
{
<summary>
Login based on login ID and password
</summary>
<param name= "Objsysadmin" ></param>
<returns></returns>
Public sysadmin adminlogin (sysadmin objsysadmin)
{
String sql = "Select AdminName from dbo. Admins WHERE loginid={0} and Loginpwd= ' {1} ';
Sql=string. Format (SQL, Objsysadmin.loginid, objsysadmin.loginpwd);
SqlDataReader SDR = sqlhelper.getreader (SQL);
if (SDR). Read ())
{
Objsysadmin.adminname = sdr["AdminName"]. ToString (); Returns the user name if the username ID and password are correct
}
Else
{
Objsysadmin = null;//Empty the object if username ID and password are incorrect
}
Sdr. Close ();
return objsysadmin;
}

}
}

Step Five: Write the controller. The controller typically consists of three steps. 1. Obtaining data 2. Business Process 3. Return data

Namespace Mvclogin.controllers
{
public class Logincontroller:controller
{
Get:login
Public ActionResult Index ()
{
Return View ("LoginView");
}
Public ActionResult Login ()
{
//[1] Get data
SysAdmin objsysadmin = new sysadmin
{
LoginId = Convert.ToInt32 (request["LoginId"]),
Loginpwd = request["Loginpwd"]. ToString ()
};
//[2] Business processing
Objsysadmin=new Sysadminservice (). Adminlogin (objsysadmin);
if (objsysadmin!=null)
{
viewdata["info"] = "welcome," + Objsysadmin.adminname; Transferring data using Viewdate
}
Else
{
viewdata["info"] = "User name or password error";
}
//[3] Return data
Return View ("LoginView");
}
}
}

C # uses the MVC framework for login verification

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.