【问题标题】:Linq2Rest authentication Asp.net WebApiLinq2Rest 身份验证 Asp.net WebApi
【发布时间】:2012-10-23 00:38:26
【问题描述】:

我正在使用具有表单授权的 Asp.net WebApi。我已经成功地创建了对控制器的 POST 请求,该请求通过使用首先进入授权控制器的 HttpClient 使用 [Authorize] 属性进行保护,授权控制器将 cookie 存储到它,客户端将其用于其他请求。对于 GET 请求,我想使用 Linq2Rest 库,它使用 JsonRestClient,但我不知道如何使用它进行授权。我很惊讶我找不到关于该主题的任何问题或讨论。修改这个库的源代码是解决这个问题的唯一方法吗?

更新: 我只使用通常的形式授权。 这就是 GET 方法的样子

    [Authorize]
    [Queryable]
    public IQueryable<Invoice> GetInvoices()
    {
        return repository.GetInvoices();
    }

【问题讨论】:

  • 将 [Authorize] 属性添加到控制器上的 GET 方法时会发生什么? IModelFilter 是否在授权之前抓取请求?请提供一些代码来表明你已经定义了你的 GET 方法。
  • @KevinJunghans 添加了更新

标签: linq authentication asp.net-mvc-4 asp.net-web-api


【解决方案1】:

我创建了自己的 RestClient 来解决这个问题,不确定是否是最好的解决方案,但这似乎对我有用。这里 currentHttpClient 是已经有认证cookie的对象。

internal class PinJsonRestClient : JsonRestClient, IRestClient
{

    private readonly HttpClient currentHttpClient;

    public PinJsonRestClient(Uri uri) : base(uri)
    {
    }

    public PinJsonRestClient(Uri uri, HttpClient currentHttpClient) : base(uri)
    {
        this.currentHttpClient = currentHttpClient;
    }

    public new Stream Get(Uri uri)
    {
        var result = currentHttpClient.GetStreamAsync(uri).Result;
        return result;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-02
    • 2014-07-27
    • 1970-01-01
    • 2014-11-28
    • 2018-05-30
    • 2014-03-20
    • 2012-06-20
    • 1970-01-01
    相关资源
    最近更新 更多