MVC + Layer 3 + ASP. NET simple login verification, mvcasp.net

Source: Internet
Author: User
Tags actionlink

MVC + Layer 3 + ASP. NET simple login verification, mvcasp.net

Create a small logon case to build a simple layer-3 MVC

Create a master page under View -- Shared:

<! DOCTYPE html> Master page

 

Note: You must use Request. IsAuthenticated for authentication. Remember to add the following code under Web. config:

<authentication mode="Forms">        <forms loginUrl="~/Account/Login" protection="All" timeout="60" path="/" />  </authentication> 

The following is the controller:AccountController. cs

 

public class AccountController : Controller      {          // GET: Account          public ActionResult Index()          {              return View();          }            public ActionResult Login()          {              return View();          }            [HttpPost]          public ActionResult Login(AccountEntity account)          {              if (new Bll.AccountManageBll().LoginCheck(account))              {                  FormsAuthentication.SetAuthCookie(account.LogID, true);                  return RedirectToAction("Index", "Home");              }              else              {                  ViewBag.msg = "LogID or Password error.";                  return View();              }          }            public ActionResult LogOut()          {              FormsAuthentication.SignOut();              return RedirectToAction("Index","Home");          }      }  
AccountController. cs

 

View:View -- Account -- Login. cshtml

@ Using Model @ model AccountEntity @ {ViewBag. Title = "Login"; Layout = "~ /Views/Shared/_ LoginPartial. cshtml ";}< br/> <br/> @ using (Html. beginForm ("Login", "Account", FormMethod. post )) {<div class = "full"> <div class = "box"> <div class = "title"> <span class = "titlespan"> <B> Log In </ b> </span> </div> <br/> <div class = "ID"> <div> <span> Login ID </span> @ Html. textBoxFor (u => u. logID) </div> <div> @ Html. validationMessageFor (u => u. logID, "", new {style = "color: # F00; font-size: 10px "}) </div> <div class = "password"> <div> <span> Password </span> @ Html. passwordFor (u => u. password) </div> <div> @ Html. validationMessageFor (u => u. password, "", new {style = "color: # F00; font-size: 10px"}) </div> <span style = "color: # ff6600; "> // verification cannot be blank </span> </div> <div class =" btnLogin "> <input type =" Submit "name =" Submit "value =" Log in "> </div> <div class =" full "> <br/> <span style =" color: # F00; font-size: 12px; font-weight: bold "> @ ViewBag. msg </span> <br/> </div>}
Login. cshtml

Create an AccountEntity object Model at the Model layer

Public class AccountEntity {[Required (ErrorMessage = "LogID cann't be empty! ")] // Required verify public string LogID {get; set;} [Required (ErrorMessage =" Password cann't be empty! ")] // Required verify public string Password {get; set;} public AccountEntity () {}// No parameter constructor public AccountEntity (string ID, string Pwd) // The constructor with parameters {LogID = ID; Password = Pwd ;}}
AccountEntity

[DAL] data access layer AccountServiceDal. cs

[This instance is used only when the database is used. the test data of this instance is on The BLL layer]

/// <Summary> /// obtain user information /// </summary> /// <returns> </returns> public List <AccountEntity> GetAccountInfo () {string sqlStr = @ "ProcSelAccount"; // List of Stored Procedure names <AccountEntity> accountList = new List <AccountEntity> (); using (SqlDataReader reader = SqlHelper. execReader (sqlStr) // SqlHelper: SQL help class {while (reader. read () {AccountEntity account = BuildSubject (reader); accountList. add (account) ;}} return accountList;} public AccountEntity BuildSubject (SqlDataReader reader) {AccountEntity account = new AccountEntity (); account. logID = reader. getString (0); account. password = reader. getString (1 );
AccountServiceDal. cs

[BLL business logic layer] AccountManagerBLL. cs

Public bool LoginCheck (AccountEntity account) {bool flag = false; // List <AccountEntity> accountList = new AccountServiceDal (). getAccountInfo (); // check user data in the database. Use this Code <span style = "color: # ff0000; "> // Test Account Data </span> List <AccountEntity> accountList = new List <AccountEntity> () // Add two user Data items {new AccountEntity (" Jarvis ", "ABC123"), new AccountEntity ("Admin", "admin123")}; foreach (AccountEntity accountInfo in accountList) {if (accountInfo. logID = account. logID & amp; accountInfo. password = account. password) {flag = true;} return flag ;}
AccountManagerBLL. cs

Logon status:

 

Verification:

Correct Logon:

Logon successful:

 

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.