相关疑难解决方法(0)

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

我正在使用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"));
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果我在AuthenticateAsync中设置它,则会返回标头,但我认为我应该在ChallengeAsync中设置它.示例实现很难找到.

c# asp.net-mvc basic-authentication asp.net-web-api asp.net-mvc-5

5
推荐指数
1
解决办法
5125
查看次数