ASP.NET MVC3角色

Rya*_*ith 1 c# asp.net roles action-filter asp.net-mvc-3

我目前正在使用MVC3框架创建一个应用程序.我理解如何使用过滤器角色,如:

[Authorize(Roles = "Admin")]
Run Code Online (Sandbox Code Playgroud)

我的问题是:

我在哪里设置角色?是登录吗?这是如何实现的?

Nic*_*ork 6

在您自己创建Forms身份验证票证时,通常会使用票证的UserData部分来存储与您的用户相关的信息.这可能是角色.

然后在Application_AuthenticateRequest事件的Global.asax中,您将解析Forms Ticket并将角色分配给当前的安全主体.

以下是与不同提供商的Forms Auth的一些指南:

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

一般来说,我通常会编写自己的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 )
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的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));                


        }
    }
Run Code Online (Sandbox Code Playgroud)

并写票:

                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);
Run Code Online (Sandbox Code Playgroud)

代码可能很难遵循,但逻辑是自定义"UserPrincipal"将自动解析Forms Auth票证的UserData部分,以获取您想要存储的信息.在我的情况下,我存储名称,角色,ID等.在我的代码中,命名空间"CAA.Utility.Security"是我的自定义Identity和Principal存储的位置.