第一:新建 Areas文件夹,Login文件夹(controllers,Models,Views) 括号里面为文件夹下的文件夹
第二:新建控制器 HomeController
public ActionResult login()
{
return View();
}
第三:在Models下新建,Login类,RetInfo类
public class Paramlogin:RetInfo//引用RetInfo类
{
/// <summary>
/// 用户名
/// </summary>
[DisplayName("用户名")]
[Required(ErrorMessage = "必填")]
public string UserName { get; set; }
//123123123
/// <summary>
/// 密码
/// </summary>
[DisplayName("密码")]
[Required(ErrorMessage = "必填")]
[StringLength(20, MinimumLength = 6, ErrorMessage = "密码长度必须在{2} 和{1}个字符")]
[DataType(DataType.Password)]
第三:在Models下新建RetInfo类
public class RetInfo
{
/// <summary>
/// 信息
/// </summary>
public string Messge { get; set; }
}
第四:login视图
@model YL.Web.Areas.login.Models.Paramlogin
</head>
<body class="gray-bg">
<div class="middle-box text-center loginscreen animated fadeInDown">
<div>
<div>
<h1 class="logo-name">YL</h1>
</div>
<h3>欢迎使用 YL</h3>
@using (Html.BeginForm("login", null, FormMethod.Post))
{
<div class="form-group">
@Html.TextBoxFor(m => m.UserName, new { @class = "form-control", @placeholder = "请输入用户名" })
@Html.ValidationMessageFor(m => m.UserName)
</div>
<div class="form-group">
@Html.TextBoxFor(m => m.UserPwd, new { @class = "form-control", @placeholder = "密码", @type = "password" })
@Html.ValidationMessageFor(m => m.UserPwd)
</div>
@Html.ValidationMessageFor(m => m.Messge)
<button type="submit" class="btn btn-primary block full-width m-b">登 录</button>
<p class="text-muted text-center">
<a href="login.html#"><small>忘记密码了?</small></a> |<a href="@Url.Action("register")">注册一个新账号</a>
</p>
}
</div>
</div>
</body>
</html>
第五:login控制器
#region 登录
/// <summary>
///
/// </summary>
/// <returns></returns>
public ActionResult login()
{
return View();
}
/// <summary>
/// 登录
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public ActionResult login(Models.Paramlogin model)
{
if (ModelState.IsValid)
{
var data = new BLL.ManagerBLL().Getlogin(model.UserName);//获取数据
if (data != null)
{
if (model.UserPwd != data.UserPassWord)//判断密码是否相等
{
ModelState.AddModelError("Messge", "登录失败,账号或密码错误");
return View();
}
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1, "",
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
"admins"
);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
System.Web.HttpCookie authCookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);
}
Session["UserInfo"] = data;//保存登录信息
return RedirectToAction("MainFrame", "Home", new { Area = "Admin" });//登录后跳转后台界面
}
else
{
ModelState.AddModelError("Messge", "登录失败,账号或密码错误");
return View();
}
}
#endregion
第五:新建base类
public class Base : Controller
{
//定义一个基类的UserInfo对象
public Manger UserInfo { get; set; }
/// <summary>
/// 重写基类在Action之前执行的方法
/// </summary>
/// <param name="filterContext"></param>
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
UserInfo = Session["UserInfo"] as Manger;
//检验用户是否已经登录,如果登录则不执行,否则则执行下面的跳转代码
if (UserInfo == null)
{
Response.Redirect("/login");
}
}
}