上次完成了管理員的登錄,這次要解決對管理員登錄后的驗證,采用AuthorizeAttribute屬性的方式。之前還要解決幾個問題,然后才重寫驗證類,最后稍微改一下界面。
目錄
一、解決問題Home控制器錯誤提示
@泰德 在評論中說瀏覽器中打開存在以下錯誤。這是因為項目中存在多個Home控制器,但系統不清楚你要訪問的是哪個控制器的內容,因此要給各路由加上命名空間。
1、首先打開 App_Start文件夾的RouteConfig文件,添加命名空間 namespaces: new string[] { "Ninesky.Website.Controllers" }
2、打開Config區域的 ConfigAreaRegistration文件,同樣添加命名空間 new string[] { "Ninesky.Website.Areas.Config.Controllers" }
3、打開Member區域的MemberAreaRegistration文件,添加命名空間 new string[] { "Ninesky.Website.Areas.Member.Controllers" }
二、建立管理員登錄驗證屬性
1、在Areas\Config文件夾上點右鍵新建類【AdminAuthorizeAttribute】,該類繼承自AuthorizeAttribute。第一步重寫AuthorizeCore,返回true則通過驗證,返回false則驗證失敗,在2.1的管理員登錄采用的session來保存管理員信息,這里只要檢查Session["Account"]是否為空就可以知道用戶是否登錄。第二步HandleUnauthorizedRequest,該方法只有驗證失敗時才會執行,這里用來在驗證跳轉到管理員登錄界面。代碼如下:
using System.Web; using System.Web.Mvc; namespace Ninesky.Website.Areas.Config { /// <summary> /// 管理員身份驗證 /// <remarks> ///創建:2014年12月16日 /// </remarks> /// </summary> public class AdminAuthorizeAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { bool _pass = false; if (httpContext.Session["Account"] != null) _pass = true; return _pass; } protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { filterContext.Result = new RedirectResult("~/Config/Administrator/Login"); } } }
2、控制器加上驗證屬性
打開config區域的Homecontroller 在控制器上添加“AdminAuthorize”屬性。
同樣為AdministratorController加上“AdminAuthorize”屬性,並為“Login”action添加”AllowAnonymous“屬性。
現在我們直接方位/Config/Home/Index 就會跳轉到登陸界面。
三、界面制作
1、稍微美化一下登錄界面。
打開Config區域Administrator的Login視圖。修改代碼如下:
@model Ninesky.Website.Areas.Config.Models.LoginViewModel @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>管理員登陸</title> </head> <body> @Styles.Render("~/Content/bootstrap.css") @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryval") @Scripts.Render("~/bundles/bootstrap") @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div id="loginBox" class="panel panel-default" style="max-width:400px; margin:auto"> <div class="panel-heading"><h4>管理員登陸</h4></div> <div class="panel-body"> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.Account, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Account, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Account, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="登陸" class="btn btn-default btn-block" /> </div> </div> </div> </div> } </body> </html> <script type="text/javascript"> $("#loginBox").css("margin-top", ($(window).height() - $("#loginBox").height()) / 2); </script>
改好后的登錄界面。
2、修改Config區域布局頁,修改后的代碼如下
<!DOCTYPE html> <html> <head> <title>網站管理 - @Page.Title</title> @Styles.Render("~/Content/bootstrap.css") @Styles.Render("~Areas/Config/Content/Style.css") @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryval") @Scripts.Render("~/bundles/bootstrap") @RenderSection("head", required: false) </head> <body> <nav class="navbar navbar-default" role="navigation"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar" aria-expanded="true" aria-controls="navbar"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand">Ninesky</a> </div> <div id="navbar" class="navbar-collapse collapse" aria-expanded="true"> <ul class="nav navbar-nav"> <li class="active"><a href="#">桌面</a></li> <li><a href="#">內容管理</a></li> <li><a href="#">用戶管理</a></li> <li><a href="#">欄目管理</a></li> <li><a href="#">網站設置</a></li> </ul> </div> </div> </nav> @RenderBody() <div class="navbar-fixed-bottom"> Copyright ©洞庭夕照 http://mzwhj.cnblogs.com</div> </body> </html>
現在可以看下登錄后的頁面。
======================