.net - 访问自定义授权 MVC4 Web Api 中的 post 或 get 参数

标签 .net asp.net-mvc-4 authorization asp.net-web-api

是否可以通过 HttpActionContext 对象访问 post 或获取参数?

我有一组传感器,用于将数据记录到提供 REST API 的 Web 服务器。我想引入某种身份验证/授权,让传感器在数据中包含其硬件 ID,然后在数据库中查找该 ID 是否存在。由于 API 提供了许多 Web api 操作方法,我理想情况下希望使用自定义授权属性

public class ApiAuthorizationFilter : AuthorizeAttribute
{
    protected override bool IsAuthorized(HttpActionContext actionContext)
    {
        return false;
    }
}

如何从actionContext访问post/get数据?

编辑: POST 示例

POST /Api/api/ActionMethod/ HTTP/1.1\r\n
Content-Type: application/json\r\n
Host: localhost\r\n
Accept: */*\r\n
Content-Length:52\r\n
\r\n
{"Id": '121a222bc', "Time": '2012-02-02 12:00:00'}\r\n

祝你有美好的一天!

最佳答案

由于其性质,AuthoriseAttribute 看起来像是在模型绑定(bind)程序和参数绑定(bind)运行之前在管道中调用的。当您访问 Request.Content 并从中读取内容时,您也会遇到问题...这只能是 done once如果您打算在 auth 属性中尝试它,您可能会破坏 mediaTypeFormater...

in WebAPI, the request body (an HttpContent) may be a read-only, infinite, non-buffered, non-rewindable stream.

更新 指定执行上下文有不同的方法... http://msdn.microsoft.com/en-us/library/system.web.http.filters.filterscope(v=vs.108).aspx 。 AuthoriseAttribute 是“全局”的,因此访问操作信息太早了。

如果您想要访问模型和参数,您可以稍微改变您的方法并使用 OnActionExecuting 过滤器(“Action”过滤器范围),并根据您的验证抛出 401 或 403。

此过滤器稍后在执行过程中调用,因此您可以完全访问绑定(bind)数据。

下面是非常简单的示例:

public class ApiAuthorizationFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        Foo model = (Foo)actionContext.ActionArguments["model"];
        string param1 = (string)actionContext.ActionArguments["param1"];
        int param2 = (int)actionContext.ActionArguments["param2"];

        if (model.Id != "1")
            throw new HttpResponseException(System.Net.HttpStatusCode.Forbidden);

        base.OnActionExecuting(actionContext);
    }
}

Controller 示例:

public class Foo
{
    public string Id { get; set; }
    public DateTime Time { get; set; }
}

public class FoosController : ApiController
{
    // PUT api/foos/5
    [ApiAuthorizationFilter]
    public Foo Put(int id, Foo model, [FromUri]string param1 = null, int? param2 = null)
    {
        return model;
    }
}

其他答案所说的......他们是对的,如果您可以在 URL 上访问所需的所有内容,则可以通过请求获取内容;但是,我认为模型和请求内容应该保持不变:

var queryStringCollection = HttpUtility.ParseQueryString(actionContext.Request.RequestUri.Query);

    //example for param1
    string param1 = queryStringCollection["param1"];
    //example for param2
    int param2 = int.Parse(queryStringCollection["param2"]);
    //Example of getting the ID from the URL
    var id = actionContext.Request.RequestUri.Segments.LastOrDefault();

关于.net - 访问自定义授权 MVC4 Web Api 中的 post 或 get 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12817202/

相关文章:

c# - MSDN 示例是否可能导致资源泄漏?

javascript - 当 View 中的函数完成时重新加载部分 View ?

authentication - 匿名用户的访问 token - JWT

api - Azure 资源管理器 API 用户的订阅范围授权

c# - Parallel.ForEach 中的迭代器在多个线程上运行

.net - edge.js:如何使用对 COM 对象的引用从 .NET 程序集中调用方法?

c# - 给定 UTF-16 大小的最大 UTF-8 字符串大小

c# - 如何在 MVC 操作中的 webgrid 列内添加隐藏字段

javascript - 更新/返回 ViewModel 返回服务器

python - 使用 Pyramid 认证