最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ASP.NET MVC自定義授權過濾器

 更新時間:2022年03月16日 11:02:50   作者:.NET開發(fā)菜鳥  
這篇文章介紹了ASP.NET MVC自定義授權過濾器的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、授權過濾器

授權過濾器用于實現(xiàn)IAuthorizationFilter接口和做出關于是否執(zhí)行操作方法(如執(zhí)行身份驗證或驗證請求的屬性)的安全策略。AuthorizeAttribute類繼承了IAuthorizationFilter接口,是授權過濾器的示例。授權過濾器在任何其他過濾器之前運行。

如果要自定義授權過濾器,只需要定義一個類繼承自AuthorizeAttribute類,然后重寫AuthorizeAttribute類里面的方法即可。

二、示例

下面根據(jù)一個具體的案例來講解如何使用自定義過濾器

1、添加對應實體類

User實體類代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCCustomerFilterDemo.Models
{
    public class User
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public int RoleId { get; set; }
    }
}

Role實體類代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCCustomerFilterDemo.Models
{
    public class Role
    {
        public int Id { get; set; }
        public string RoleName { get; set; }
        public string Description { get; set; }
    }
}

RoleWithControllerAction實體類代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCCustomerFilterDemo.Models
{
    public class RoleWithControllerAction
    {
        public int Id { get; set; }
        public string ControllerName { get; set; }
        public string ActionName { get; set; }
        public string RoleIds { get; set; }
    }
}

用于展示登錄視圖的登錄用戶實體類LogOnViewModel代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;

namespace MVCCustomerFilterDemo.Models
{
    // <summary>
    /// 用戶登錄類
    /// </summary>
    public class LogOnViewModel
    {
        /// <summary>
        /// 用戶名
        /// </summary>
        [DisplayName("用戶名")]
        public string UserName { get; set; }

        /// <summary>
        /// 密碼
        /// </summary>
        [DisplayName("密碼")]
        public string Password { get; set; }

        /// <summary>
        /// 記住我
        /// </summary>
        [DisplayName("記住我")]
        public bool RememberMe { get; set; }

    }
}

2、添加測試數(shù)據(jù)

在程序中模擬數(shù)據(jù)庫中的數(shù)據(jù),實際使用中要去數(shù)據(jù)庫查詢,代碼如下:

using MVCCustomerFilterDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCCustomerFilterDemo.DataBase
{
    /// <summary>
    /// 測試數(shù)據(jù)(實際項目中,這些數(shù)據(jù)應該從數(shù)據(jù)庫拿)
    /// </summary>
    public class SampleData
    {
        public static List<User> users;
        public static List<Role> roles;
        public static List<RoleWithControllerAction> roleWithControllerAndAction;

        static SampleData()
        {
            // 初始化用戶
            users = new List<User>()
            {
                new User(){ Id=1, UserName="jxl", RoleId=1},
                new User(){ Id=2, UserName ="senior1", RoleId=2},
                new User(){ Id=3, UserName ="senior2", RoleId=2},
                new User(){ Id=5, UserName="junior1", RoleId=3},
                new User(){ Id=6, UserName="junior2", RoleId=3},
                new User(){ Id=6, UserName="junior3", RoleId=3}
            };
            // 初始化角色
            roles = new List<Role>()
            {
                new Role() { Id=1, RoleName="管理員", Description="管理員角色"},
                new Role() { Id=2, RoleName="高級會員", Description="高級會員角色"},
                new Role() { Id=3, RoleName="初級會員", Description="初級會員角色"}
            };
            // 初始化角色控制器和Action對應類
            roleWithControllerAndAction = new List<RoleWithControllerAction>()
            {
                new RoleWithControllerAction(){ Id=1, ControllerName="AuthFilters", ActionName="AdminUser", RoleIds="1"},
                new RoleWithControllerAction(){ Id=2, ControllerName="AuthFilters", ActionName="SeniorUser",RoleIds="1,2"},
                new RoleWithControllerAction(){ Id=3, ControllerName="AuthFilters", ActionName="JuniorUser",RoleIds="1,2,3"},
                new RoleWithControllerAction(){ Id=3, ControllerName="AuthFilters", ActionName="Welcome",RoleIds="1,2"},
                new RoleWithControllerAction(){ Id=4, ControllerName="ActionFilters", ActionName="Index", RoleIds="2,3"},
                new RoleWithControllerAction(){ Id=4, ControllerName="ActionPremisFilters", ActionName="Index", RoleIds="2,3"}
            };
        }
    }
}

3、新建繼承類

新建一個UserAuthorize類,繼承自AuthorizeAttribute類,然后F12轉到定義查看AuthorizeAttribute代碼,代碼如下:

#region 程序集 System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// E:\Practice\過濾器\自定義權限過濾器\MVCCustomerFilterDemo\packages\Microsoft.AspNet.Mvc.5.2.4\lib\net45\System.Web.Mvc.dll
#endregion

namespace System.Web.Mvc
{
    //
    // 摘要:
    //     指定對控制器或操作方法的訪問只限于滿足授權要求的用戶。
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class AuthorizeAttribute : FilterAttribute, IAuthorizationFilter
    {
        //
        // 摘要:
        //     初始化 System.Web.Mvc.AuthorizeAttribute 類的新實例。
        public AuthorizeAttribute();

        //
        // 摘要:
        //     獲取或設置有權訪問控制器或操作方法的用戶角色。
        //
        // 返回結果:
        //     有權訪問控制器或操作方法的用戶角色。
        public string Roles { get; set; }
        //
        // 摘要:
        //     獲取此特性的唯一標識符。
        //
        // 返回結果:
        //     此特性的唯一標識符。
        public override object TypeId { get; }
        //
        // 摘要:
        //     獲取或設置有權訪問控制器或操作方法的用戶。
        //
        // 返回結果:
        //     有權訪問控制器或操作方法的用戶。
        public string Users { get; set; }

        //
        // 摘要:
        //     在過程請求授權時調(diào)用。
        //
        // 參數(shù):
        //   filterContext:
        //     篩選器上下文,它封裝有關使用 System.Web.Mvc.AuthorizeAttribute 的信息。
        //
        // 異常:
        //   T:System.ArgumentNullException:
        //     filterContext 參數(shù)為 null。
        public virtual void OnAuthorization(AuthorizationContext filterContext);
        //
        // 摘要:
        //     重寫時,提供一個入口點用于進行自定義授權檢查。
        //
        // 參數(shù):
        //   httpContext:
        //     HTTP 上下文,它封裝有關單個 HTTP 請求的所有 HTTP 特定的信息。
        //
        // 返回結果:
        //     如果用戶已經(jīng)過授權,則為 true;否則為 false。
        //
        // 異常:
        //   T:System.ArgumentNullException:
        //     httpContext 參數(shù)為 null。
        protected virtual bool AuthorizeCore(HttpContextBase httpContext);
        //
        // 摘要:
        //     處理未能授權的 HTTP 請求。
        //
        // 參數(shù):
        //   filterContext:
        //     封裝有關使用 System.Web.Mvc.AuthorizeAttribute 的信息。filterContext 對象包括控制器、HTTP 上下文、請求上下文、操作結果和路由數(shù)據(jù)。
        protected virtual void HandleUnauthorizedRequest(AuthorizationContext filterContext);
        //
        // 摘要:
        //     在緩存模塊請求授權時調(diào)用。
        //
        // 參數(shù):
        //   httpContext:
        //     HTTP 上下文,它封裝有關單個 HTTP 請求的所有 HTTP 特定的信息。
        //
        // 返回結果:
        //     對驗證狀態(tài)的引用。
        //
        // 異常:
        //   T:System.ArgumentNullException:
        //     httpContext 參數(shù)為 null。
        protected virtual HttpValidationStatus OnCacheAuthorization(HttpContextBase httpContext);
    }
}

從AuthorizeAttribute的源代碼中可以看出:里面定義了Users和Roles兩個屬性,只需要給這兩個屬性賦值,就可以控制用戶或角色訪問了。要實現(xiàn)自定義的驗證只需要重寫OnAuthorization和AuthorizeCore方法。所以,UserAuthorize類代碼如下:

using MVCCustomerFilterDemo.DataBase;
using MVCCustomerFilterDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCCustomerFilterDemo.Extensions
{
    public class UserAuthorize : AuthorizeAttribute
    {
        /// <summary>
        /// 授權失敗時呈現(xiàn)的視圖
        /// </summary>
        public string AuthorizationFailView { get; set; }

        /// <summary>
        /// 請求授權時執(zhí)行
        /// </summary>
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            // 判斷是否已經(jīng)驗證用戶
            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                // 如果沒有驗證則跳轉到LogOn頁面
                filterContext.HttpContext.Response.Redirect("/Account/LogOn");
            }

            //獲得url請求里的controller和action:
            string strControllerName = filterContext.RouteData.Values["controller"].ToString().ToLower();
            string strActionName = filterContext.RouteData.Values["action"].ToString().ToLower();

            //根據(jù)請求過來的controller和action去查詢可以被哪些角色操作:
            Models.RoleWithControllerAction roleWithControllerAction =
                SampleData.roleWithControllerAndAction.Find(r => r.ControllerName.ToLower() == strControllerName &&
                r.ActionName.ToLower() == strActionName);

            if (roleWithControllerAction != null)
            {
                //有權限操作當前控制器和Action的角色id
                this.Roles = roleWithControllerAction.RoleIds;    
            }

            base.OnAuthorization(filterContext);
        }
        /// <summary>
        /// 自定義授權檢查(返回False則授權失?。?
        /// </summary>
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {

            if (httpContext.User.Identity.IsAuthenticated)
            {
                //當前登錄用戶的用戶名
                string userName = httpContext.User.Identity.Name;
                //當前登錄用戶對象
                User user = SampleData.users.Find(u => u.UserName == userName);   

                if (user != null)
                {
                    //當前登錄用戶的角色
                    Role role = SampleData.roles.Find(r => r.Id == user.RoleId);  
                    foreach (string roleid in Roles.Split(','))
                    {
                        if (role.Id.ToString() == roleid)
                            return true;
                    }
                    return false;
                }
                else
                    return false;
            }
            else
            {
                //進入HandleUnauthorizedRequest
                return false;      
            }

        }

        /// <summary>
        /// 處理授權失敗的HTTP請求
        /// </summary>
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            filterContext.Result = new ViewResult { ViewName = AuthorizationFailView };
        }
    }
}

4、添加Account控制器

Account控制器里面的LogOn方法用來顯示登陸界面,控制器代碼如下:

using MVCCustomerFilterDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace MVCCustomerFilterDemo.Controllers
{
    public class AccountController : Controller
    {

        // GET: Account
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 顯示登錄視圖
        /// </summary>
        /// <returns></returns>
        public ActionResult LogOn()
        {
            LogOnViewModel model = new LogOnViewModel();
            return View(model);

        }

        /// <summary>
        /// 處理用戶點擊登錄提交回發(fā)的表單
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult LogOn(LogOnViewModel model)
        {
            //只要輸入的用戶名和密碼一樣就過
            if (model.UserName.Trim() == model.Password.Trim())
            {
                // 判斷是否勾選了記住我
                if (model.RememberMe)
                {
                    //2880分鐘有效期的cookie
                    FormsAuthentication.SetAuthCookie(model.UserName, true);
                }
                else
                {
                    //會話cookie
                    FormsAuthentication.SetAuthCookie(model.UserName, false);
                }
                // 跳轉到AuthFilters控制器的Welcome方法
                return RedirectToAction("Welcome", "AuthFilters");
            }
            else
            {
                return View(model);
            }

        }

        /// <summary>
        /// 注銷
        /// </summary>
        /// <returns></returns>
        public ActionResult LogOut()
        {
            Session.Abandon();
            FormsAuthentication.SignOut();
            return RedirectToAction("LogOn");
        }
    }
}

LogOn方法對應的視圖頁面代碼如下:

@model MVCCustomerFilterDemo.Models.LogOnViewModel
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>LogOn</title>
</head>
<body>
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

        <div class="form-horizontal">
            <h4>登錄</h4>
            <hr />
            @Html.ValidationSummary(true)

            <div class="form-group">
                @Html.LabelFor(model => model.UserName, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.UserName)
                    @Html.ValidationMessageFor(model => model.UserName)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Password)
                    @Html.ValidationMessageFor(model => model.Password)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.RememberMe, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.RememberMe)
                    @Html.ValidationMessageFor(model => model.RememberMe)
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="登錄" class="btn btn-default" />
                </div>
            </div>
        </div>
    }
</body>
</html>

5、修改配置文件

修改配置文件,定義權限驗證失敗時跳轉的頁面,代碼如下:

<!--配置登錄頁面-->
<authentication mode="Forms">
    <forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>

6、添加授權控制器

添加AuthFilters控制器,代碼如下:

using MVCCustomerFilterDemo.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCCustomerFilterDemo.Controllers
{
    public class AuthFiltersController : Controller
    {
        // GET: AuthFilters
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 使用自定義的授權驗證,登錄成功就可以訪問
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult Welcome()
        {
            return View();
        }
        [UserAuthorize(AuthorizationFailView = "Error")]
        public ActionResult AdminUser()
        {
            ViewBag.Message = "管理員頁面";
            return View("Welcome");
        }

        /// <summary>
        /// 會員頁面(管理員、會員都可訪問)
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [UserAuthorize(AuthorizationFailView = "Error")]   
        public ActionResult SeniorUser()
        {
            ViewBag.Message = "高級會員頁面";
            return View("Welcome");
        }

        /// <summary>
        /// 游客頁面(管理員、會員、游客都可訪問)
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [UserAuthorize(AuthorizationFailView = "Error")]   
        public ActionResult JuniorUser()
        {
            ViewBag.Message = "初級會員頁面";
            return View("Welcome");
        }
    }
}

三、測試

測試Welcome

Welcome這個Action使用了默認的授權驗證,只要登錄成功就能訪問。

URL地址欄里面輸入:http://localhost:****/AuthFilters/Welcome,會跳轉到登錄頁面,如圖所示:

然后輸入相同的用戶名和密碼,點擊登錄,會顯示W(wǎng)elcome對應的頁面:

在看一下SampleData中,角色為1,2的也可以訪問Welcome方法,用角色1訪問Welcome:

點擊登錄:

從上面的截圖中看出:senior1登錄成功了,senior1是角色2,證明角色1、2可以訪問Welcome方法。在使用junior2登錄名訪問Welcome方法:

由于junior2的角色是3,而角色3沒有訪問Welcome方法的權限,所以會跳轉到Error頁面:

四、總結

Welcome這個Action使用了默認的授權驗證,只要登陸成功就可以訪問。其他幾個Action上都標注了自定義的UserAuthorize,并沒有標注Users="....",Roles=".....",因為這樣在Action上寫死用戶或者角色控制權限顯然是不可行的,用戶和角色的對應以及不同的角色可以操作的Action應該是從數(shù)據(jù)庫里取出來的。為了演示就在SampleData類里初始化了一些用戶和角色信息,根據(jù)SampleData類的定義,很明顯jxl擁有1號管理員角色,可以訪問AuthFilters這個控制器下的所有Action;senior1、senior2擁有2號高級會員的角色,可以訪問AuthFilters這個控制器下除了AdminUser之外的Action等等。
再次登陸下,就發(fā)現(xiàn)擁有高級會員角色的用戶senior1是不可以訪問AdminUser這個Action,會被帶到AuthorizationFailView屬性指定的Error視圖。

GitHub代碼地址:https://github.com/jxl1024/MVCCustomerFilterDemo

到此這篇關于ASP.NET MVC自定義授權過濾器的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 在ASP.NET使用JavaScript顯示信息提示窗口實現(xiàn)原理及代碼

    在ASP.NET使用JavaScript顯示信息提示窗口實現(xiàn)原理及代碼

    在ASP.NET使用JavaScript顯示信息窗口,感興趣的朋友可以了解一下,本文將介紹詳細的操作步驟,希望對你的JavaScript知識鞏固有所幫助
    2013-01-01
  • .NET中的repeater簡介及分頁效果

    .NET中的repeater簡介及分頁效果

    Repeater控件是一個數(shù)據(jù)綁定容器控件,它能夠生成各個項的列表,并可以使用模板定義網(wǎng)頁上各個項的布局。本文對此進行詳細介紹,下面跟著小編一起來看下吧
    2017-02-02
  • asp.net 用戶控件中圖片及樣式問題

    asp.net 用戶控件中圖片及樣式問題

    我們在開發(fā)網(wǎng)站的大多時候都會用到用戶控件,不同功能的aspx文件要放到不同的文件夾,但我們可能要引用同一個用戶控件
    2009-05-05
  • .net core 靜態(tài)類獲取appsettings的方法

    .net core 靜態(tài)類獲取appsettings的方法

    這篇文章主要介紹了.net core 靜態(tài)類獲取appsettings的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • 比較完整的 asp.net 學習流程

    比較完整的 asp.net 學習流程

    好多朋友想學習后臺編程語言,但請注意的事,學習后臺是個循序漸進的過程,不可能一下就到位,其實不只是asp.net其它的編程語言都需要下面的一些知識。
    2009-06-06
  • asp.net利用HttpModule實現(xiàn)防sql注入

    asp.net利用HttpModule實現(xiàn)防sql注入

    關于sql注入,已經(jīng)被很多人討論過了。這篇沒有新意功能也不夠通用,nnd,不想引起口水,就是覺得簡單而且思路有參考性才貼出來。
    2009-12-12
  • asp.net core項目中如何使用html文件

    asp.net core項目中如何使用html文件

    這篇文章主要給大家介紹了關于asp.net core項目中如何使用html文件的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面是隨著小編來一起學習學習吧。
    2018-02-02
  • ASP.NET中Web.config文件的層次關系詳細介紹

    ASP.NET中Web.config文件的層次關系詳細介紹

    Web.config 是一個基于 XML 的配置文件,該文件的作用是對應用程序進行配置,下面為大家介紹下ASP.NET中Web.config文件的層次關系
    2014-01-01
  • .net core整合log4net的解決方案

    .net core整合log4net的解決方案

    這篇文章主要給大家介紹了關于.net core整合log4net的解決方案,文中通過圖文介紹的非常詳細,對大家學習或者使用.net core具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-07-07
  • ASP.NET MVC+EF框架+EasyUI實現(xiàn)權限管系列

    ASP.NET MVC+EF框架+EasyUI實現(xiàn)權限管系列

    在學習MVC之前,我們有必要知道這些知識點(自動屬性,隱式類型var,對象初始化器和集合初始化器,匿名類,擴展方法,Lambda表達式),如果你還不知道的話就請看我下面的簡單的介紹,看下面我建立的項目的初步圖像,然后下篇我們開始簡單的介紹。
    2014-11-11

最新評論

泊头市| 濮阳市| 高州市| 白银市| 荆门市| 宣武区| 邳州市| 云龙县| 常德市| 潮州市| 手游| 五莲县| 财经| 揭东县| 台山市| 信丰县| 枝江市| 舒兰市| 浑源县| 抚远县| 曲周县| 安吉县| 鄂托克旗| 大厂| 正安县| 阳新县| 页游| 六枝特区| 荥经县| 钟祥市| 读书| 德格县| 建昌县| 沽源县| 万盛区| 信丰县| 庄浪县| 淮北市| 奎屯市| 静安区| 醴陵市|