繁体   English   中英

ASP.NET MVC3角色

[英]ASP.NET MVC3 Roles

我目前正在使用MVC3框架创建一个应用程序。 我了解如何将角色与过滤器结合使用,例如:

[Authorize(Roles = "Admin")]

我的问题是:

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

自己创建表单身份验证票证时,通常会使用票证的UserData部分来存储与用户有关的信息。 这可能是角色。

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

以下是与其他提供商进行的表单身份验证的一些指南:

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 )
    {
    }
}

在您的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”是存储我的自定义身份和主体的位置。

我在哪里设置角色?

这将取决于您在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表中设置角色。 您可以看看以下文章 但是,如果您使用的是自定义角色提供程序,则将取决于此提供程序的实现方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM