MVC+三层+ASP.NET简单登录验证
通过制作一个登录小案例来搭建MVC简单三层

在View --Shared下创建一个母版页:
<!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<link href="~/Content/LogIn.css" rel="stylesheet" />
<style type="text/css">
* {
margin: ;
padding: ;
} .nav {
border: 2px solid border-right:none;
overflow: hidden;
float: center;
} .nav ul li {
float: left;
width: 130px;
height: 20px;
} .nav ul li a {
width: 150px;
height: 30px;
text-align: center;
line-height: 28px;
display: block;
border-right: 2px solid #FFFFFF;
color: #FFF;
width: 150px;
font-weight: bold;
} .nav ul li a:hover {
font-weight: bold;
color: #FFFFFF;
} .nav ul li ul {
position: absolute;
display: none;
} .nav ul li ul li {
float: none;
} .nav ul li ul li a {
width: 150px;
height: 40px;
border-right: none;
border-top: 1px dotted #FFFFFF;
background: #cc6698;
} .nav ul li:hover ul {
display: block;
} .barStyle {
color: #FFFFFF;
font-weight: bold;
} .style1 {
text-align: center;
font-family: "Meiryo UI";
font-size: x-large;
}
</style>
<title>@ViewBag.Title</title>
</head>
<body>
<div id="header" style="background: #a6154c; width: 100%; height: 80px">
<p style="color: #ffffff; padding: 23px" class="style1">
<strong>User Management System</strong>
</p>
</div> @if (Request.IsAuthenticated) <span style="color:#ff6600;"> //如果登陆成功</span>
{
<center>
<div class="nav" style="background: #a6154c; width: 100%; height: 30px">
<ul>
<li>@Html.ActionLink("Home", "Index", "Home")</li> //主页
<li>@Html.ActionLink("Log Out", "LogOut", "Account")</li> //导航栏添加一个 log Out 选项
</ul>
</div>
<div><span>Current ID: @Context.User.Identity.Name</span> </div> <span style="color:#ff6600;">//获取当前登录用户</span>
</center>
}
else <span style="color:#ff6600;">// 未登录状态</span>
{
<center>
<div class="nav" style="background: #a6154c; width: 100%; height: 30px">
<ul>
<li>@Html.ActionLink("Home", "Index", "Home")</li> //主页
<li>@Html.ActionLink("Log In", "login", "Account")</li> //登陆选项
</ul>
</div>
<b>Pls login first!</b> //提示需要先登录
</center>
} <div>
@RenderBody()
</div>
</body>
</html>
母版页
注意:需要使用Request.IsAuthenticated身份验证,记得在Web.config下添加如下代码
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" protection="All" timeout="" path="/" />
</authentication>
接下来是控制器: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--Account--Login.cshtml
@using Model
@model AccountEntity
@{
ViewBag.Title = "Login";
Layout = "~/Views/Shared/_LoginPartial.cshtml";
} <br />
<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>
<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;"> //校验不能为空</span>
</div>
<div class="btnLogin">
<input type="Submit" name="Submit" value="Log In">
</div>
</div>
</div>
<div class="full">
<br />
<span style="color:#F00;font-size:12px;font-weight:bold">@ViewBag.msg</span><br />
</div>
}
Login.cshtml
Model层新建一个AccountEntity实体模型
public class AccountEntity
{
[Required(ErrorMessage ="LogID cann't be empty!")] //Required 验证
public string LogID { get; set; } [Required(ErrorMessage = "Password cann't be empty!")] //Required 验证
public string Password { get; set; } public AccountEntity() { } //无参构造函数
public AccountEntity(string ID,string Pwd) //有参构造函数 为了测试数据
{
LogID = ID;
Password = Pwd;
}
}
AccountEntity
【DAL】数据访问层AccountServiceDal.cs
【使用数据库才需要使用,本实例测试数据在BLL层】
/// <summary>
/// 获取用户信息
/// </summary>
/// <returns></returns>
public List<AccountEntity> GetAccountInfo()
{
string sqlStr = @"ProcSelAccount"; //存储过程名
List<AccountEntity> accountList = new List<AccountEntity>(); using (SqlDataReader reader = SqlHelper.ExecReader(sqlStr)) //SqlHelper: SQL帮助类
{
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();
account.Password = reader.GetString();
AccountServiceDal.cs
【BLL业务逻辑层】AccountManagerBLL.cs
public bool LoginCheck(AccountEntity account)
{
bool flag = false;
// List<AccountEntity> accountList = new AccountServiceDal().GetAccountInfo(); //校验数据库中用户数据,需使用此代码 <span style="color:#ff0000;">//Test Account Data</span>
List<AccountEntity> accountList = new List<AccountEntity>() //增加两条用户数据
{
new AccountEntity("Jarvis","ABC123"),
new AccountEntity("Admin","admin123")
}; foreach (AccountEntity accountInfo in accountList)
{
if(accountInfo.LogID == account.LogID && accountInfo.Password == account.Password)
{
flag = true;
}
}
return flag;
}
AccountManagerBLL.cs
登录状态:

验证:

正确登录:

登录成功:

MVC+三层+ASP.NET简单登录验证的更多相关文章
- 【ADO.NET】2、各种版本的 简单登录验证
一.简单登录验证(防SQL注入) GetString(序号) 返回某一列的值(当用户不记得列名序号时,可使用GetOrdinal()获取到序号)GetInt32(序号) 针对的是 int 字段,返回i ...
- ASP.NET简单登录注册实例
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...
- asp.net mvc3登录验证
1,在web.config中 <system.web>节点下面增加: <authentication mode="Forms"> <forms na ...
- Python第一天-----简单登录验证
----------------------------------------- 编写登录接口 要求:1.输入用户名密码 2.认证成功后显示欢迎信息 3.输错三次后锁定 -------------- ...
- nodejs(一) 简单登录验证 使用mongoose 操作MongoDB
---恢复内容开始--- 开发使用webstorm 9 新建nodejs+express 项目 newfarmer 文章目录 配置Mongoose 创建目录及文件 插入数据,POST提交JSON增加 ...
- nutz中实现登录验证
一.nutz是什么 nutz是一个轻便的web端开发框架.主页如下:http://www.nutzam.com/core/nutz_preface.html 二.session简单介绍 大家都知道ht ...
- Asp.NetMVC利用LigerUI搭建一个简单的后台管理详解(函登录验证)
上一篇 Asp.Net 中Grid详解两种方法使用LigerUI加载数据库数据填充数据分页 了解了LigerUI 中Grid的基本用法 现在结合上一篇的内容做一个简单的后台管理,当然也有前台的页面 ...
- 程序猿修仙之路--数据结构之你是否真的懂数组? c#socket TCP同步网络通信 用lambda表达式树替代反射 ASP.NET MVC如何做一个简单的非法登录拦截
程序猿修仙之路--数据结构之你是否真的懂数组? 数据结构 但凡IT江湖侠士,算法与数据结构为必修之课.早有前辈已经明确指出:程序=算法+数据结构 .要想在之后的江湖历练中通关,数据结构必不可少. ...
- ASP.NET MVC 登录验证
好久没写随笔了,这段时间没 什么事情,领导 一直没安排任务,索性 一直在研究代码,说实在的,这个登录都 搞得我云里雾里的,所以这次我可能也讲得不是 特别清楚,但是 我尽力把我知道的讲出来,顺便也对自 ...
随机推荐
- 王之泰201771010131《面向对象程序设计(java)》第二周学习总结
王之泰201771010131<面向对象程序设计(java)>第二周学习总结 第一部分:理论知识学习部分 第三章 第三章内容主要为Java语言的基础语法,主要内容如下 1.基础知识 1.1 ...
- 请求headers处理
有时在请求一个网页内容时,发现无论通过GET或者是POST以及其他请求方式,都会出现403错误.这种现象多数是由于服务器拒绝了您的访问,那是因为这些网页为了防止恶意采集信息,所使用的反爬虫设置.此时可 ...
- css的再深入6(更新中···)
background-position 雪碧图 我们的html和css中有三个属性可以向服务器发送请求,src href url. overflow (1) 值hidden 超出就隐藏 (2) 值s ...
- 第十届蓝桥杯2019年C/C++ 大学B组省赛试题
2019年第十届蓝桥杯大赛软件类省赛C/C++大学B组 试题 A:组队 本题总分:5分 [问题描述] 作为篮球队教练,你需要从以下名单中选出 1号位至 5号位各一名球员, 组成球队的首发阵容. 每位球 ...
- FJUT3701 这也是一道数论题(线段树)题解
Problem Description 好久没出数据结构题,现在赶紧来做道数据结构题热热身 小q现在要去银行,他有个很厉害的bug能看到前面排队的人.假如当前有人正在办理业务,那么肯定要等待前一个人完 ...
- Linux 查看进程之PS命令
要对进程进行检测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程运行状态.Linux 系统中 我们可以使用 ps 命令查看进程. ps 命令介绍 ps(process stat ...
- Qt信号和槽机制
概述 信号和槽机制是QT的核心机制,要精通QT编程就必须对信号和槽有所了解.信号和槽是一种高级接口,应用于对象之间的通信,他是QT的核心特性,也是QT差别于其他工具包的重要地方.信号和槽是QT自行定义 ...
- antd Select进阶功能 动态更新、函数防抖
一.动态更新Options Antd Select自带的搜索功能很多时候需要结合后端的接口,输入一个关键字的时候会自动更新选择器的选项. 下面列一些注意点 基础实现 选择器选项必须和每次更新的数据挂钩 ...
- 《HTTP 权威指南》笔记:第十四章 安全 HTTP
 HTTPS 与 HTTP 不同,其在传输层与应用层之间添加了一个 SSL/TLS 的安全层.机制:所有的 HTTP 请求与响应都要通过 SSL/TLS 先进行加密,再进行传输. 基础知识 密码 c ...
- linux存储管理之文件系统
EXT3/4文件系统 ====================================================================================Ext3: ...