MVC身份驗證及權限管理

MVC自帶的ActionFilter 數據庫

在Asp.Net WebForm的中要作到身份認證微軟爲咱們提供了三種方式,其中最經常使用的就是咱們的Form認證,須要配置相應的信息。例以下面的配置信息: 數組

<authentication mode="Forms">
    <forms loginUrl="Login.aspx" defaultUrl="Default.aspx" protection="All" />
</authentication>
<authorization>
    <deny users="?"/>
    <allow users="*"/>
</authorization>

說明咱們登陸頁面是Login.aspx,登陸成功後的默認頁面是Default.aspx,而咱們用戶信息採用驗證和加密兩種方式。並且最重要的 是咱們要寫好受權方式(下面的受權必定要寫不然只說明使用Forms認證而後設置相關屬性是沒有用的),拒絕全部匿名用戶,只有登陸用戶能夠正常訪問。這 樣以後咱們設置點擊登陸按鈕將用戶名寫進cookie(也就是執行FormsAuthentication.SetAuthCookie(name, false);)就能夠了。 cookie

在Asp.Net MVC中咱們一樣可使用Forms認證,可是若是你按照WebForm中的作法去作就不行了。例如你這樣配置信息: ide

<authentication mode="Forms">
    <forms loginUrl="~/Account/Login" defaultUrl="~/Home/Index" protection="All"/>
</authentication>
<authorization>
    <deny users="?"/>
    <allow users="*"/>
</authorization>

你在Login.aspx中設置登陸來觸發AccountController中的Logon來登陸,其中Logon代碼: 函數

public ActionResult Logon(string name,string password)    
{    
    if (name == "jianxin160" && password == "160796")    
    {    
        FormsAuthentication.SetAuthCookie(name, false);    
        return Redirect("~/Home/Index");    
    }    
    else
    {    
        return Redirect("/");    
    }    
}

這樣的操做以後你會發現你的Logon是不會執行的。緣由是什麼呢?怎麼一樣的設置爲何到了MVC中就不行了?緣由就是兩者機制不一樣,由於你設置的受權方式讓Logon沒法訪問了。那麼咱們怎麼來作呢? ui

其實在Asp.Net MVC中咱們有更好的方式來作這一切,咱們不須要受權方式,也就是說咱們的配置信息像這樣: 加密

<authentication mode="Forms">
    <forms loginUrl="~/Account/Login" defaultUrl="~/Home/Index" protection="All"/>
</authentication>

不須要說明匿名用戶不能登陸等。固然了,你會發現僅僅就這樣作確定不行的咱們還要換一種方式告訴系統哪些是須要登陸才能訪問的。你或許 想,o(︶︿︶)o 唉,那太麻煩了吧。其實不是這樣的,很簡單,咱們只須要在須要認證的Action上標記[Authorize]就能夠了。例如我在Home文件夾中有兩個 頁面Index和Home,我如今想讓Index通過認證才能訪問,而Home不須要,那麼只須要給Index這個Action標記 [Authorize],也就是: spa

[Authorize]    
public ActionResult Index()    
{    
    return View();    
}    

public ActionResult Home()    
{    
    return View();    
}

這樣Index就必須登陸以後才能訪問,而Home是不須要登陸的。若是你須要進行角色受權那麼您就能夠在標記Authorize的時候指明角色 (例如[Authorize(Role=Administrators)] ),不過您這是就必須使用微軟給咱們提供的Membership機制了,由於您的Role不多是無緣無故有的,而是存在於對應的數據庫中的,這個我在另 一篇博客中提到過就很少說了。 code

自定義ActionFilter orm

有時候這樣的認證或許您還不可以知足,或者說您以爲不夠靈活,那麼也沒有關係,Asp.Net MVC是容許您自定義ActionFilter的。例如我如今自定義身份認證:

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

namespace FormFormsAuthenticationMvc   
{   
    public class RequiresAuthenticationAttribute:ActionFilterAttribute   
    {   
        public override void  OnActionExecuting(ActionExecutingContext filterContext)   
        {   
            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)   
            {   
                string returnUrl = filterContext.HttpContext.Request.Url.AbsolutePath;   
                string redirectUrl = string.Format("?ReturnUrl={0}", returnUrl);   
                string loginUrl = FormsAuthentication.LoginUrl + redirectUrl;   
                filterContext.HttpContext.Response.Redirect(loginUrl, true);   
            }   
        }    
    }   
}

若是須要進行用戶管理,我再定義角色相關的Filter:

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

namespace MvcApplication1.MyClass    
{    
    public class RequiresRoleAttribute:ActionFilterAttribute    
    {    
        public string Role { get; set; }    

        public override void OnActionExecuting(ActionExecutingContext filterContext)    
        {    
            if (!string.IsNullOrEmpty(Role))    
            {    
                if (!filterContext.HttpContext.User.Identity.IsAuthenticated)    
                {    
                    string returnUrl = filterContext.HttpContext.Request.Url.AbsolutePath;    
                    string redirectUrl = string.Format("?ReturnUrl={0}", returnUrl);    
                    string loginUrl = FormsAuthentication.LoginUrl + redirectUrl;    
                    filterContext.HttpContext.Response.Redirect(loginUrl, true);    
                }    
                else
                {    
                    bool isAuthenticated = filterContext.HttpContext.User.IsInRole(Role);    
                    if (!isAuthenticated)    
                    {    
                    throw new UnauthorizedAccessException("You have no right to view the page!");    
                    }    
                }    
            }    
            else
            {    
                throw new InvalidOperationException("No Role Specified!");    
            }    
        }    
    }    
}

其實您會發現上面兩個Attribute其實MVC自帶的Authorized已經解決了,這裏主要告訴你們若是有須要您是能夠擴展的。

好了,今天就到這裏吧!源代碼下載:FormFormsAuthenticationMvc

 

ASP.NET MVC 創建 ASP.NET 基礎之上,不少 ASP.NET 的特性(如窗體身份驗證、成員資格)在 MVC 中能夠直接使用。本文旨在提供可參考的代碼,不會涉及這方面太多理論的知識。

本文僅使用 ASP.NET 的窗體身份驗證,不會使用它的 成員資格(Membership) 和 角色管理 (RoleManager),緣由有二:一是不靈活,二是和 MVC 關係不太。

1、示例項目

image

User.cs 是模型文件,其中包含了 User 類:

public class User
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Password { get; set; }
    public string[] Roles { get; set;  }
}

UserRepository 爲數據存取類,爲了演示方便,並無鏈接數據庫,而是使用一個數組來做爲數據源:

public class UserRepository
{
    private static User[] usersForTest = new[]{
        new User{ ID = 1, Name = "bob", Password = "bob", Roles = new []{"employee"}},
        new User{ ID = 2, Name = "tom", Password = "tom", Roles = new []{"manager"}},
        new User{ ID = 3, Name = "admin", Password = "admin", Roles = new[]{"admin"}},
    };

    public bool ValidateUser(string userName, string password)
    {
        return usersForTest
            .Any(u => u.Name == userName && u.Password == password);
    }

    public string[] GetRoles(string userName)
    {
        return usersForTest
            .Where(u => u.Name == userName)
            .Select(u => u.Roles)
            .FirstOrDefault();
    }

    public User GetByNameAndPassword(string name, string password)
    {
        return usersForTest
            .FirstOrDefault(u => u.Name == name && u.Password == password);
    }
}

2、用戶登陸及身份驗證

方式一

修改 AccountController:原有 AccountController 爲了實現控制反轉,對窗體身份驗證進行了抽象。爲了演示方便,我去除了這部分(以及註冊及修改密碼部分):

public class AccountController : Controller
{
    private UserRepository repository = new UserRepository();
    
    public ActionResult LogOn()
    {
        return View();
    }

    [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (repository.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                if (!String.IsNullOrEmpty(returnUrl)) return Redirect(returnUrl);
                else return RedirectToAction("Index", "Home");
            }
            else
                ModelState.AddModelError("", "用戶名或密碼不正確!");
        }
        return View(model);
    }

    public ActionResult LogOff()
    {
        FormsAuthentication.SignOut();
        return RedirectToAction("Index", "Home");
    }
}

修改 Global.asax:

public class MvcApplication : System.Web.HttpApplication
{
    public MvcApplication()
    {
        AuthorizeRequest += new EventHandler(MvcApplication_AuthorizeRequest);
    }

    void MvcApplication_AuthorizeRequest(object sender, EventArgs e)
    {
        IIdentity id = Context.User.Identity;
        if (id.IsAuthenticated)
        {
            var roles = new UserRepository().GetRoles(id.Name);
            Context.User = new GenericPrincipal(id, roles);
        }
    }
    //...
}

給 MvcApplication 增長構造函數,在其中增長 AuthorizeRequest 事件的處理函數。

代碼下載:Mvc-FormsAuthentication-RolesAuthorization-1.rar (243KB)

方式二

此方式將用戶的角色保存至用戶 Cookie,使用到了 FormsAuthenticationTicket。

修改 AccountController:

public class AccountController : Controller
{
    private UserRepository repository = new UserRepository();
    
    public ActionResult LogOn()
    {
        return View();
    }

    [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            User user = repository.GetByNameAndPassword(model.UserName, model.Password);
            if (user != null)
            {
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                    1,
                    user.Name,
                    DateTime.Now,
                    DateTime.Now.Add(FormsAuthentication.Timeout),
                    model.RememberMe,
                    user.Roles.Aggregate((i,j)=>i+","+j)
                    );                    
                HttpCookie cookie = new HttpCookie(
                    FormsAuthentication.FormsCookieName,
                    FormsAuthentication.Encrypt(ticket));
                Response.Cookies.Add(cookie);

                if (!String.IsNullOrEmpty(returnUrl)) return Redirect(returnUrl);
                else return RedirectToAction("Index", "Home");
            }
            else
                ModelState.AddModelError("", "用戶名或密碼不正確!");
        }
        return View(model);
    }

    public ActionResult LogOff()
    {
        FormsAuthentication.SignOut();
        return RedirectToAction("Index", "Home");
    }
}

修改 Global.asax:

public class MvcApplication : System.Web.HttpApplication
{
    public MvcApplication()
    {
        AuthorizeRequest += new EventHandler(MvcApplication_AuthorizeRequest);
    }

    void MvcApplication_AuthorizeRequest(object sender, EventArgs e)
    {
        var id = Context.User.Identity as FormsIdentity;
        if (id != null && id.IsAuthenticated)
        {
            var roles = id.Ticket.UserData.Split(',');
            Context.User = new GenericPrincipal(id, roles);
        }
    }
    //...
}

代碼下載:Mvc-FormsAuthentication-RolesAuthorization-2.rar (244KB)

3、角色權限

使用任一種方式後,咱們就能夠在 Controller 中使用 AuthorizeAttribute 實現基於角色的權限管理了:

[Authorize(Roles = "employee,manager")]
public ActionResult Index1()
{
    return View();
}
[Authorize(Roles = "manager")]
public ActionResult Index2()
{
    return View();
}
[Authorize(Users="admin", Roles = "admin")]
public ActionResult Index3()
{
    return View();
}

4、簡要說明

MVC 使用 HttpContext.User 屬性進行來進行實現身份驗證及角色管理,一樣 AuthorizeAttribute 也根據 HttpContext.User 進行角色權限驗證。

因些不要在用戶登陸後,將相關用戶信息保存在 Session 中(網上常常看到這種作法),將用戶保存在 Session 中是一種很是很差的作法。

也不要在 Action 中進行角色權限判斷,應該使用 AuthorizeAttribute 或它的子類,如下的方式都是錯誤的:

public ActionResult Action1()
{
    if (Session["User"] == null) { /**/}
    /**/
}
public ActionResult Action2()
{
    if (User.Identity == null) { /**/}
    if (User.Identity.IsAuthenticated == false) { /**/}
    if (User.IsInRole("admin") == false) { /**/}
    /**/
}
相關文章
相關標籤/搜索