第一步:需要在webconfig下面的
注意~/Account/Login为你的登陆界面。
第二步编写BaseController,代码如下
[SupportFilter]
public class BaseController : Controller
{
public LogonModel CurrentUser
{
get
{
if (Session["account"] != null)
return Session["account"] as LogonModel;
return null;
}
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (HttpContext.User.Identity.IsAuthenticated && Session["account"] == null)
{
var u = new LogonModel { LoginName="admin", Password="123", RememberMe=false };
Session["account"] = u;
}
base.OnActionExecuting(filterContext);
}
protected bool IsContainRoleName(string roleName)
{
return false;
}
}
第三步:BaseController新增一个SupportFilter的过滤器,用来过滤一些没有通过验证的,代码如下:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
filterContext.HttpContext.Response.Write(" ");
filterContext.Result = new EmptyResult();
return;
}
else
{
//编写通过验证的代码
}
}
第四步:编写登陆验证代码
// GET: Account
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(LogonModel model)
{
if (ModelState.IsValid)
{
var u = model;
if (u != null)
{
FormsAuthentication.SetAuthCookie(u.LoginName, false);
Session["account"] = u;
return RedirectToAction("Index", "Home");
}
}
ModelState.AddModelError("LoginName", "验证失败");
return View();
}
第五步:登陆页面
@model WebApplication4.Models.LogonModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
Index
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
LogonModel
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.LoginName, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.LoginName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LoginName, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.ValidateCode, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.ValidateCode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ValidateCode, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.RememberMe, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.RememberMe)
@Html.ValidationMessageFor(model => model.RememberMe, "", new { @class = "text-danger" })
}
@Html.ActionLink("Back to List", "Index")
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
第六步:需要身份验证的Controller继承BaseController
public class HomeController : BaseController
{
public ActionResult Index()
{
ViewData["userName"] = base.CurrentUser.LoginName;
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}