ASP.NET MVC 使用 Authorize 屬性過濾器驗證使用者是否已登入
阿新 • • 發佈:2020-10-23
ASP.NET MVC 使用 Authorize 過濾器驗證使用者登入。Authorize 過濾器首先執行在任何其它過濾器或動作方法之前,主要用來做登入驗證或者許可權驗證。
示例:使用 Authorize 過濾器實現簡單的使用者登入驗證。
1、建立登入控制器 LoginController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
///<summary>
///登入控制器
///</summary> [AllowAnonymous]
publicclassLoginController:Controller
{
///<summary>
///登入頁面
///</summary>
publicActionResultIndex()
{
return View();
}
///<summary>
///登入
///</summary>
[HttpPost]
publicActionResultLogin(stringloginName,stringloginPwd)
{
if (loginName== "admin" &&loginPwd== "123456" )
{
//登入成功
Session[ "LoginName" ]=loginName;
return RedirectToAction( "Index" , "Home" );
}
else
{
//登入失敗
return RedirectToAction( "Index" , "Login" );
}
}
///<summary>
///登出
///</summary>
publicActionResultLogout()
{
Session.Abandon();
return RedirectToAction( "Index" , "Login" );
}
}
|
注意:在登入控制器 LoginController 上新增 AllowAnonymous 特性,該特性用於標記在授權期間要跳過 AuthorizeAttribute 的控制器和操作。
2、建立登入頁面
1 2 3 4 5 6 7 8 9 10 11 12 |
@{
ViewBag.Title= "登入頁面" ;
Layout= null ;
}
<h2>登入頁面</h2>
<formaction= '@Url.Action("Login","Login")' id= "form1" method= "post" >
使用者:<inputtype= "text" name= "loginName" /><br/>
密碼:<inputtype= "password" name= "loginPwd" /><br/>
<inputtype= "submit" value= "登入" >
</form>
|
效果圖:
3、建立主頁控制器 LoginController
1 2 3 4 5 6 7 8 9 10 |
publicclassHomeController:Controller
{
publicActionResultIndex()
{
//獲取當前登入使用者
stringloginName=Session[ "LoginName" ].ToString();
ViewBag.Message= "當前登入使用者:" +loginName;
return View();
}
}
|
4、建立主頁頁面
1 2 3 4 5 6 7 8 |
@{
ViewBag.Title= "Index" ;
Layout= null ;
}
<h2>Index</h2>
<h3>@ViewBag.Message</h3>
<ahref= "@Url.Action(" Logout "," Login ")" >登出</a>
|
效果圖:
5、建立授權過濾器 LoginAuthorizeAttribute 類
建立 Filter 目錄,在該目錄下建立授權過濾器 LoginAuthorizeAttribute 類,繼承 AuthorizeAttribute。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
usingSystem.Web.Mvc;
namespaceMvcApp.Filter
{
///<summary>
///授權過濾器
///</summary>
publicclassLoginAuthorizeAttribute:AuthorizeAttribute
{
publicoverridevoidOnAuthorization(AuthorizationContextfilterContext)
{
//判斷是否跳過授權過濾器
if (filterContext.ActionDescriptor.IsDefined( typeof (AllowAnonymousAttribute), true )
||filterContext.ActionDescriptor.ControllerDescriptor.IsDefined( typeof (AllowAnonymousAttribute), true ))
{
return ;
}
//判斷登入情況
if (filterContext.HttpContext.Session[ "LoginName" ]== null ||filterContext.HttpContext.Session[ "LoginName" ].ToString()== "" )
{
//HttpContext.Current.Response.Write("認證不通過");
//HttpContext.Current.Response.End();
filterContext.Result= new RedirectResult( "/Login/Index" );
}
}
}
}
|
通常 Authorize 過濾器也是在全域性過濾器上面的,在 App_Start 目錄下的 FilterConfig 類的 RegisterGlobalFilters 方法中新增:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
usingSystem.Web;
usingSystem.Web.Mvc;
usingMvcApp.Filter;
namespaceMvcApp
{
publicclassFilterConfig
{
publicstaticvoidRegisterGlobalFilters(GlobalFilterCollectionfilters)
{
filters.Add( new HandleErrorAttribute());
//新增全域性授權過濾器
filters.Add( new LoginAuthorizeAttribute());
}
}
}
|
Global.asax 下的程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Mvc;
usingSystem.Web.Optimization;
usingSystem.Web.Routing;
namespaceMvcApp
{
publicclassMvcApplication:System.Web.HttpApplication
{
protectedvoidApplication_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
|