在ASP.NET MVC号楼CustomAuthorization [英] Building CustomAuthorization in ASP.NET MVC

查看:119
本文介绍了在ASP.NET MVC号楼CustomAuthorization的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DB 角色用户一个实体一对多的关系。

我所试图做的是建立自定义授权过滤器。我已经使用默认的 ASP.NET 会员看到的所有教程。所有我知道的是,我需要继承 AuthorizationAttribute 但不知道哪些方法做我需要覆盖以及如何实现它们。

 公共类USERAUTH:AuthorizeAttribute
{}

DB

角色

 公共类角色
{
    [键]
    公众诠释角色ID {搞定;组; }    [需要]
    公众诠释RolenameValue {搞定;组; }    [MAXLENGTH(100)
    公共字符串描述{搞定;组; }    // // // // //    公共ROLENAME ROLENAME
    {
        {返回(ProjectName.Domain.Enums.Rolename)RolenameValue; }
        集合{RolenameValue =(int)的值; }
    }    公共虚拟的ICollection<使用者>用户{搞定;组; }
}

用户

 公共类用户
{
    [键]
    公众诠释用户名{搞定;组; }    [需要]
    [MAXLENGTH(30)]
    公共字符串用户名{获得;组; }    [需要]
    [MINLENGTH个(5)]
    公共字符串密码{搞定;组; }    [需要]
    [数据类型(DataType.EmailAddress)
    公共字符串电子邮件{获得;组; }    [MAXLENGTH(30)]
    公共字符串名字{获得;组; }    [MAXLENGTH(50)]
    公共字符串名字{获得;组; }    [数据类型(DataType.Date)
    公众的DateTime生日{搞定;组; }    公众诠释GenderValue {搞定;组; }    // // // // // // //    公共性别性别
    {
        {返回(ProjectName.Domain.Enums.Gender)GenderValue; }
        集合{GenderValue =(int)的值; }
    }    公众诠释角色ID {搞定;组; }    [ForeignKey的(角色ID)]
    公共角色角色{搞定;组; }


解决方案

您并不需要创建一个自定义属性。您可以使用现有 AuthoriseAttribute 但你应该做的是实现自定义的,将使用自己的角色从DB校长类。在你的主要类,你将实现IsInRole方法:

 公共BOOL IsInRole(字符串角色)
{
    如果(this.Roles == NULL)
        this.Roles = DependencyResolver.Current
           .GetService< ISecurityService>()
           .GetUserPermissions(this.Identity.Name);    返回this.Roles.Any(p值=> p.Name ==作用);
}

您应该设置你自定义的主要在Global.asax中

 无效OnPostAuthenticateRequest(对象发件人,EventArgs的发送)
    {
         //获取当前用户参考
        IPrincipal的用户= HttpContext.Current.User;        //如果我们正在处理一个身份验证的窗体身份验证请求
        如果(user.Identity.IsAuthenticated&安培;&安培; user.Identity.AuthenticationType ==表单)
        {
            //创建自定义主
            VAR本金=新MyCustomPrincipal(user.Identity);            //附加委托方HttpContext.User中和Thread.CurrentPrincipal中
            HttpContext.Current.User =本金;
            System.Threading.Thread.CurrentPrincipal =本金;
        }
    }

In the DB i have Role and User entities with one to many relationship.

What i am trying to do is to build custom authorization filter. All the tutorials that i have seen are using default ASP.NET membership. All i know is that i need to inherit AuthorizationAttribute but do not know which methods do i need to override and how to implement them.

public class UserAuth : AuthorizeAttribute
{

}

In the DB:

Role

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; }
}

User

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; }

解决方案

You don't need to create a custom attribute. You can use existing AuthoriseAttribute but what you should do is implement custom Principal class that will use your own roles from DB. In your Principal class you will implement IsInRole method:

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

You should set your custom Principal in Global.asax

    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; 
        }
    } 

这篇关于在ASP.NET MVC号楼CustomAuthorization的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆