访问ApiController键入DelegatingHandler [英] Accessing ApiController Type in DelegatingHandler

查看:134
本文介绍了访问ApiController键入DelegatingHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Asp.Net的WebAPI的一个项目。我目前工作的认证和授权。

I am using Asp.Net WebAPI for a project. I am currently working on authentication and authorization.

我有一个的MessageHandler,将检查请求的HTTP认证头,并建立了我的身份和用户配置文件。不过,我想有注释声称,行动可能需要(我们有很多的用户可以拥有债权,所以我不希望加载它们全部)我的控制器操作(或只是控制器)。

I have a messageHandler that will check the HTTP authentication header of a request and build my identity and user profile. However, I want to annotate my controller action (or just the controller) with claims that the action may require (we have a lot of claims that a user can have, so I don't want to load them all).

例如:

public class MyController : ApiController
{
    [LoadClaims("SomeClaim", "SomeOtherClaim", "etc")]
    public string Get()
    {
        if (HasClaim("SomeClaim"))
            return "Awesome";

        return "Bummer";
    }
}

认证消息处理程序我希望能够看看属性和提出索赔从DB基于背面仅需要什么内幕。为此,我需要知道什么控制器和动作我会根据航线命中:

Inside the authentication message handler I want to be able to look at the attributes and bring claims back from the DB based on only what is required. For that I need to know what Controller and Action I will hit based on route:

 protected override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
...

            var routeData = request.GetRouteData();
            object controllerName;
            object actionName;
            routeData.Values.TryGetValue("controller", out controllerName);
...

所以我可以得到的。但现在我需要把它变成一个类型,我可以反映,但我已经是控制器的名字(甚至没有完整的类名或命名空间)。我怎么可以变成的东西,我可以反映获得属性等等呢?

So I can get that. But now I need to turn this into a Type that I can reflect on, but all I have is the controller name (not even the full class name or namespace). How can I turn this into something that I can reflect on to get attributes etc?

我期待到 DefaultHttpControllerSelector 来看到的WebAPI堆栈是怎么做到的,而且它似乎使用 HttpControllerTypeCache 。这是一个内部类,所以我不能创建一个实例。什么是去获得目标控制器类型的正确方法是什么?

I am looking into DefaultHttpControllerSelector to see how the WebAPI stack does it, and it seems to use HttpControllerTypeCache. This is an internal class so I can't create an instance. What is the correct way to go about getting the target controller Type?

推荐答案

您可以访问类型使用全局服务定位器解析器自己。

You can get access to the type resolver yourself using the global service locator.

var controllerTypeResolver = GlobalConfiguration.Configuration.Services.GetHttpControllerTypeResolver();
var controllerTypes = controllerTypeResolver.GetControllerTypes(GlobalConfiguration.Configuration.Services.GetAssembliesResolver());
var controllerType = controllerTypes.SingleOrDefault(ct => ct.Name == string.Format("{0}Controller", controllerName));

您可能会想这样做的结果一些缓存(如控制器选择一样)。但是,这种方法应该工作。

You will probably want to do some caching of the results (like the controller selector does). But this approach should work.

但是

您可能会更好移动这个逻辑到自定义过滤器的授权,控制器,而不是委托处理程序上坐。鉴于你需要知道控制器的类型,你不妨让ControllerSelector正常工作。也许,如果你把你的负载要求属性为授权行动过滤器属性,你可以只加载作为参数传入的索赔,并设置本金和索赔那里呢?

You may be better off moving this logic into a Custom authorisation filter that sits on the controller rather than a delegating handler. Given you need to know the controller type you may as well let the ControllerSelector work normally. Perhaps, if you turned your load claims attribute into an authorization action filter attribute you could just load the claims passed in as parameters and set the principal and claims there and then?

这篇关于访问ApiController键入DelegatingHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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