2012-01-23 67 views
1

我目前正在創建一個使用MVC3框架的應用程序。我知道如何使用角色與像過濾器:ASP.NET MVC3角色

[Authorize(Roles = "Admin")] 

我的問題是:

在哪裏設置的角色?它是否登錄?這是如何實現的?

+0

您使用什麼會員供應商?您是使用其中一個內置提供程序,還是僅使用表單身份驗證票證? –

+0

我正在使用表單身份驗證票據,應用程序是從頭建立的,所以我不知道會員提供商? –

回答

6

當您自己創建Forms Authentication工單時,您通常會使用工單的UserData部分存儲與您的用戶有關的信息。這可能是角色。

然後在Application.AuthenticateRequest事件的Global.asax中,您將分析您的Forms票單並將角色分配給當前安全主體。

這裏是表單一些導遊身份驗證與不同的供應商:

http://weblogs.asp.net/scottgu/archive/2006/02/24/ASP.NET-2.0-Membership_2C00_-Roles_2C00_-Forms-Authentication_2C00_-and-Security-Resources-.aspx

總的來說,我usualy寫我自己的System.Security.Principal.GenericPrincipal和System.Web.Security.FormsIdentity做所有的爲我工作。

public class UserIdentity: System.Web.Security.FormsIdentity 
{ 
    public string[] Roles { get; private set; } 
    public string FirstName { get; private set; } 
    public string UserName { get; private set; } 
    public int UserID { get; private set; } 

    public UserIdentity(System.Web.Security.FormsAuthenticationTicket ticket) : base(ticket) 
    { 
     if (ticket.UserData != null && ticket.UserData.IndexOf("|") != -1) 
     { 
      string[] dataSections = ticket.UserData.Split('|'); 

      //Get the first name 
      FirstName = dataSections.Length >= 3 ? dataSections[2] : ""; 

      //Get the username 
      UserName = ticket.Name; 

      #region Parse the UserID 
      int userID = 0; 
      int.TryParse(dataSections[0], out userID); 
      this.UserID = userID; 
      #endregion 

      this.Roles = System.Text.RegularExpressions.Regex.Split(dataSections[1], ","); 

     } 
    } 
} 

public class UserPrincipal : System.Security.Principal.GenericPrincipal 
{ 
    public UserPrincipal(UserIdentity identity) : base(identity, identity.Roles) 
    { 
    } 
} 

而在你的Global.asax:

protected void Application_AuthenticateRequest(object sender, EventArgs e) 
    { 
     if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated && HttpContext.Current.User.Identity is System.Web.Security.FormsIdentity) 
     { 

      HttpContext.Current.User = new CAA.Utility.Security.UserPrincipal(HttpContext.Current.User.Identity is CAA.Utility.Security.UserIdentity? HttpContext.Current.User.Identity as CAA.Utility.Security.UserIdentity : new Utility.Security.UserIdentity(((System.Web.Security.FormsIdentity)HttpContext.Current.User.Identity).Ticket));     


     } 
    } 

而寫票:

   System.Web.Security.FormsAuthenticationTicket ticket = new System.Web.Security.FormsAuthenticationTicket(1, user.Username, DateTime.Now, DateTime.Now.AddDays(1), false, String.Format("{0}|{1}|{2}", user.UserID ,user.Roles.ToString(), user.FirstName), System.Web.Security.FormsAuthentication.FormsCookiePath); 
       HttpCookie cookie = new HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName, System.Web.Security.FormsAuthentication.Encrypt(ticket)); 

       if (model.RememberMe) 
        cookie.Expires = ticket.Expiration; 


       Response.Cookies.Add(cookie); 

的代碼可能會難以遵循,但邏輯是自定義「UserPrincipal 「將自動解析Forms Auth票據的UserData部分,以獲取您想要存儲的信息。在我的情況下,我存儲名稱,角色,ID等。在我的代碼中,名稱空間「CAA.Utility.Security」是我的自定義標識和主體存儲位置。

+0

完美,正是我期待的! –

2

我在哪裏設置角色?

這將取決於您在web.config中使用的角色提供程序。如果您使用的是默認AspNetSqlRoleProvider提供商:

<roleManager enabled="false"> 
    <providers> 
    <clear/> 
    <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" /> 
    <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" /> 
    </providers> 
</roleManager> 

然後設置你的aspnet_Roles表的作用。你可以看看following article。但是,如果您使用自定義角色提供程序,那麼它將取決於此提供程序如何實現。