首页 新闻 赞助 找找看

获取Web API中自定义验证的属性传值问题

0
[已解决问题] 解决于 2014-07-08 11:24

自定义验证类代码如下:

 public class RoleAuthorizeAttribute : AuthorizeAttribute
    {
        public string RoleEntity { get; set; }

        private Dictionary<int, List<string>> userRole = new Dictionary<int, List<string>>();

        public override void OnAuthorization(HttpActionContext actionContext)
        {
            userRole.Clear();
            userRole.Add(1, new List<string> { "admin", "guest", "anmo" });
            userRole.Add(2, new List<string> { "guest", "anmo" });
            userRole.Add(3, new List<string> { "admin", "anmo" });

            base.OnAuthorization(actionContext);
          
            if (CacheHelper.Get("userid") != null)
            {
                if (userRole.ContainsKey(int.Parse(CacheHelper.Get("userid").ToString())))
                {
                    var roleList = userRole[int.Parse(CacheHelper.Get("userid").ToString())];
                    if (!roleList.Contains(RoleEntity))
                    {
                        HandleUnauthorizedRequest(actionContext);
                    }
                }

            }
            else
            {
                HandleUnauthorizedRequest(actionContext);
            }
        }

        protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
        {
            base.HandleUnauthorizedRequest(actionContext);

            var challengeMessage = new HttpResponseMessage(HttpStatusCode.Unauthorized);
            challengeMessage.Headers.Add("www-Authenticate", "basic");
            throw new HttpResponseException(challengeMessage);
        }


    }

在controller中:

       [RoleAuthorize(RoleEntity = "admin")]
        public List<ChannelEntity> GetInfo(string code)
        {
          
            return null;
        }

 

问题是,在RoleAuthorizeAttribute类的OnAuthorization方法中,RoleEntity始终都是null,请问这是怎么回事

scotly的主页 scotly | 初学一级 | 园豆:4
提问于:2014-07-08 10:21
< >
分享
最佳答案
1

你没有构造函数。

奖励园豆:5
Launcher | 高人七级 |园豆:45045 | 2014-07-08 10:29

public RoleAuthorizeAttribute()
{
}

加了构造函数,还是空的

scotly | 园豆:4 (初学一级) | 2014-07-08 10:31

@scotly: public RoleAuthorizeAttribute(string roleEntity) : base()
{

RoleEntity = roleEntity;
}

Launcher | 园豆:45045 (高人七级) | 2014-07-08 10:34

@Launcher: 不行,这样也是空

scotly | 园豆:4 (初学一级) | 2014-07-08 10:37

@Launcher: 感觉一直获取的是WebApiConfig注册的时候的值  

config.Filters.Add(new RoleAuthorizeAttribute(string.Empty));

scotly | 园豆:4 (初学一级) | 2014-07-08 10:39

@Launcher: 刚才调试了一下,没有进构造函数,直接走到了OnAuthorization方法

scotly | 园豆:4 (初学一级) | 2014-07-08 10:43

@scotly: 你看下 AuthorizationContext 的这两个属性中的值:Properties 和 ClaimSets

Launcher | 园豆:45045 (高人七级) | 2014-07-08 10:44

@Launcher: 

properties是在actionContext.ActionDescriptor里面,为空

claimSets不知道在哪

scotly | 园豆:4 (初学一级) | 2014-07-08 10:47

@scotly: 请你告诉我下,你的 AuthorizeAttribute 是在哪个程序集中?

Launcher | 园豆:45045 (高人七级) | 2014-07-08 10:50

@Launcher: System.Web.Http

scotly | 园豆:4 (初学一级) | 2014-07-08 10:51

@Launcher: 刚才试了一下,发现赋值成功了,但是进入OnAuthorization方法后,立马就变成空了

scotly | 园豆:4 (初学一级) | 2014-07-08 10:53

@scotly: 把 config.Filters.Add(new RoleAuthorizeAttribute(string.Empty)); 这句注释掉。

Launcher | 园豆:45045 (高人七级) | 2014-07-08 10:57

@Launcher: 可以了,这个是什么原因,难道在WebApiConfig中不需要注册吗,我看很多说明都需要加上的

scotly | 园豆:4 (初学一级) | 2014-07-08 11:02

@scotly: 我给你提个醒儿,默认的 AuthorizeAttribute 可以这么用:

[Authorize(Users="Alice,Bob")]

[Authorize(Roles="Administrators")]

也就是说已经提供了设置受限用户或角色的方式,你可以直接使用。

Launcher | 园豆:45045 (高人七级) | 2014-07-08 11:02

@scotly: 

1,config.Filters.Add 是给所有 Controller 设置同一个 AuthorizeFiler;

2, 将 [Authorize] 放置在 Controller 上给该 Controller 设置;

3,将 [Authorize] 放置在 Controller.ActionXXX 上给该某个方法设置;

Launcher | 园豆:45045 (高人七级) | 2014-07-08 11:04

@Launcher: 如果在全局设置以后,再在方法中设置,不会覆盖全局的值吗,比如所有进入系统的都是管理员,然后对每一个方法添加细分管理员

scotly | 园豆:4 (初学一级) | 2014-07-08 11:08

@scotly: 说句实话,我都没写过你这个代码,我只是根据你提供的程序集在 MSDN 上查找到了该类的相关 API 说明。根据说明,我建议你这么做,不要自定义,用下面的代码来检验:

config.Filters.Add(new AuthorizeAttribute());

[Authorize(Roles="Administrators")]

public YourxxxxController: ApiController

{

    [Authorize(Roles="SupperAdministrators")]

    void ActionXXX();

}

然后你测试下是否会覆盖全局设置。

Launcher | 园豆:45045 (高人七级) | 2014-07-08 11:13

@Launcher: 嗯,谢谢,我写role放这,是因为项目自定义了角色、功能、权限,需要在这验证

scotly | 园豆:4 (初学一级) | 2014-07-08 11:17

@scotly: 我还是再提醒你一句,默认的 Authorize 已经有 Roles 和 Users 了,而通过 HttpActionContext 也能读取到 Action (即你的功能),因此重写 OnAuthorization 可以完全利用这些已有的信息。我不知道你是怎么学习此框架的,但是从我的角度来看,你应该学习下基于角色、基于申明的认证和授权流程,学会使用 IPrincipal 和 ClaimSet 。

Launcher | 园豆:45045 (高人七级) | 2014-07-08 11:32

@Launcher: 嗯,好的,我正在看这方面的资料, 非常感谢

scotly | 园豆:4 (初学一级) | 2014-07-08 11:37
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册