C# ASP.NET MVC3角色

C# ASP.NET MVC3角色,c#,asp.net,asp.net-mvc-3,roles,action-filter,C#,Asp.net,Asp.net Mvc 3,Roles,Action Filter,我目前正在使用MVC3框架创建一个应用程序。我了解如何将角色与过滤器一起使用,如: [Authorize(Roles = "Admin")] 我的问题是: 我在哪里设置角色?是登录的吗?这是如何实现的 我在哪里设置角色 这将取决于您在web.config中使用的角色提供程序。如果您使用的是默认的AspNetSqlRoleProvider提供程序: <roleManager enabled="false"> <providers> <clear/>

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

[Authorize(Roles = "Admin")]
我的问题是:

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

我在哪里设置角色

这将取决于您在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>

. 但是,如果您使用的是自定义角色提供程序,那么它将取决于该提供程序是如何实现的

我在哪里设置角色

这将取决于您在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>


. 但是,如果您使用的是自定义角色提供程序,则它将取决于此提供程序的实现方式。

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

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

以下是有关不同提供者的表单验证的一些指南:

一般来说,我通常编写自己的System.Security.Principal.GenericPrincipal和System.Web.Security.FormSidenty来为我完成所有工作

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”是存储我的自定义标识和主体的位置。

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

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

以下是有关不同提供者的表单验证的一些指南:

一般来说,我通常编写自己的System.Security.Principal.GenericPrincipal和System.Web.Security.FormSidenty来为我完成所有工作

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”是存储我的自定义标识和主体的位置。

您使用的是什么成员资格提供程序?您是在使用内置的提供者还是只是在使用表单身份验证票证?我在使用表单身份验证票证,应用程序是从头开始构建的,所以我不知道成员资格提供者?您使用的是什么成员资格提供者?你是在使用内置的提供者,还是仅仅使用表单身份验证票证?我使用表单身份验证票证,应用程序是从头开始构建的,所以我不知道成员资格提供者?太好了,这正是我想要的!完美,正是我想要的!