如何在IAuthenticationFilter实现中设置WWW-Authentication标头?

杰米·艾德(Jamie Ide)

我正在使用MVC5的IAuthenticationFilter接口实现基本身份验证。我的理解是,现在这是首选方法,而不是使用DelegatingHandler。我已经工作了,但响应中未返回www-authenticate标头。这是我对ChallengeAsync的实现:

public async Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
    {
        var result = await context.Result.ExecuteAsync(cancellationToken);
        if (result.StatusCode == HttpStatusCode.Unauthorized)
        {
            result.Headers.WwwAuthenticate.Add(new AuthenticationHeaderValue("Basic", "realm=localhost"));
        }
    }

如果我在AuthenticateAsync中设置了标头,则返回该标头,但我认为应该在ChallengeAsync中对其进行设置。示例实现很难找到。

巴德里

在中ChallengeAsync,将其设置context.Result为type的实例IHttpActionResult,如下所示。

public Task ChallengeAsync(HttpAuthenticationChallengeContext context,
                                  CancellationToken cancellationToken)
{
    context.Result = new ResultWithChallenge(context.Result);
    return Task.FromResult(0);
}

提供一个实现,就像这样。

public class ResultWithChallenge : IHttpActionResult
{
    private readonly IHttpActionResult next;

    public ResultWithChallenge(IHttpActionResult next)
    {
        this.next = next;
    }

    public async Task<HttpResponseMessage> ExecuteAsync(
                                CancellationToken cancellationToken)
    {
        var response = await next.ExecuteAsync(cancellationToken);
        if (response.StatusCode == HttpStatusCode.Unauthorized)
        {
            response.Headers.WwwAuthenticate.Add(
                   new AuthenticationHeaderValue("Basic", "realm=localhost"));
        }

        return response;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何用Ruby Savon编写SOAP Authentication标头

来自分类Dev

标头X-DocuSign-Authentication应该如何用于REST和SOAP?

来自分类Dev

如何在HTTP响应中设置标头?

来自分类Dev

如何在Swift中设置UIWebView的“用户代理”标头

来自分类Dev

如何在Guzzle中设置引荐来源标头

来自分类Dev

如何在jQuery中设置请求标头

来自分类Dev

如何在vue-cli中设置HTTP标头?

来自分类Dev

如何在NestJS响应中设置标头字段?

来自分类Dev

如何在DRF的APIClient()delete()请求中设置标头?

来自分类Dev

如何在Apache JClouds中设置HTTP标头?

来自分类Dev

如何在SoapHttpClientProtocol中设置User-Agent标头?

来自分类Dev

如何在httpRequest中设置标头身份验证?

来自分类Dev

如何在magento中设置http标头

来自分类Dev

如何在php的SoapClient中设置和禁用标头?

来自分类Dev

如何在角度 2 中设置 beforesend 标头

来自分类Dev

如何在葡萄中传递标头?

来自分类Dev

RSpec:设置请求标头

来自分类Dev

BIS设置HTTP标头

来自分类Dev

PHP自行设置标头

来自分类Dev

NodeJS错误-发送标头后无法设置标头

来自分类Dev

异步响应-发送标头后无法设置标头

来自分类Dev

Express-发送标头后无法设置标头

来自分类Dev

表达错误:发送标头后无法设置标头

来自分类Dev

错误:发送标头后无法设置标头

来自分类Dev

收到错误-发送标头后无法设置标头

来自分类Dev

如何实现把手条件标头

来自分类Dev

在仅标头库中隐藏实现

来自分类Dev

@interface中的实例变量;标头与实现

来自分类Dev

标头与C ++中的实现不同