【问题标题】:Building CustomAuthorization in ASP.NET MVC在 ASP.NET MVC 中构建 CustomAuthorization
【发布时间】:2012-06-01 00:23:00
【问题描述】:

DB 中,我有RoleUser 具有一对多关系的实体。

我想做的是构建自定义授权过滤器。我看到的所有教程都使用默认的ASP.NET 成员资格。我只知道我需要继承AuthorizationAttribute,但不知道我需要重写哪些方法以及如何实现它们。

public class UserAuth : AuthorizeAttribute
{

}

DB

角色

public class Role
{
    [Key]
    public int RoleID { get; set; }

    [Required]
    public int RolenameValue { get; set; }

    [MaxLength(100)]
    public string Description { get; set; }

    // // // // //

    public Rolename Rolename 
    {
        get { return (ProjectName.Domain.Enums.Rolename)RolenameValue; }
        set { RolenameValue = (int)value; }
    }

    public virtual ICollection<User> Users { get; set; }
}

用户

public class User
{
    [Key]
    public int UserID { get; set; }

    [Required]
    [MaxLength(30)]
    public string Username { get; set; }

    [Required]
    [MinLength(5)]
    public string Password { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [MaxLength(30)]
    public string FirstName { get; set; }

    [MaxLength(50)]
    public string LastName { get; set; }

    [DataType(DataType.Date)]
    public DateTime Birthdate { get; set; }

    public int GenderValue { get; set; }

    // // // // // // //

    public Gender Gender
    {
        get { return (ProjectName.Domain.Enums.Gender)GenderValue; }
        set { GenderValue = (int)value; }
    }

    public int RoleID { get; set; }

    [ForeignKey("RoleID")]
    public Role Role { get; set; }

【问题讨论】:

    标签: asp.net asp.net-mvc c#-4.0 authorization custom-attributes


    【解决方案1】:

    您不需要创建自定义属性。您可以使用现有的AuthoriseAttribute,但您应该实现自定义Principal 类,该类将使用您自己的DB 角色。在您的 Principal 类中,您将实现 IsInRole 方法:

    public bool IsInRole(string role)
    {
        if(this.Roles == null)
            this.Roles = DependencyResolver.Current
               .GetService<ISecurityService>()
               .GetUserPermissions(this.Identity.Name);
    
        return this.Roles.Any(p => p.Name == role);
    }
    

    您应该在 Global.asax 中设置您的自定义 Principal

        void OnPostAuthenticateRequest(object sender, EventArgs e)
        {
             // Get a reference to the current User 
            IPrincipal user = HttpContext.Current.User; 
    
            // If we are dealing with an authenticated forms authentication request         
            if (user.Identity.IsAuthenticated && user.Identity.AuthenticationType == "Forms") 
            { 
                // Create custom Principal 
                var principal = new MyCustomPrincipal(user.Identity); 
    
                // Attach the Principal to HttpContext.User and Thread.CurrentPrincipal 
                HttpContext.Current.User = principal; 
                System.Threading.Thread.CurrentPrincipal = principal; 
            }
        } 
    

    【讨论】:

    • +1,我以相同的方式执行此操作,只是我通过OnPostAtuthenticate 中的授权存储库加载所有角色。
    • 如何实现MyCustomPrincipal? :P 编辑:我的意思是,如何正确实施它。只有IsInRole() ?
    • 派生自GenericPrincipal (msdn.microsoft.com/en-us/library/…) 或实现IPrincipal 接口
    猜你喜欢
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多