C# 在Owin中间件中设置挑战

C# 在Owin中间件中设置挑战,c#,asp.net,asp.net-identity,owin,katana,C#,Asp.net,Asp.net Identity,Owin,Katana,Owin中间件实现在添加质询之前查找自己的身份验证类型,因此只有适当的中间件响应。可以同时使用多个挑战 protected override Task ApplyResponseChallengeAsync() { if (Response.StatusCode == 401) { var challenge = Helper.LookupChallenge(Options.AuthenticationType, Options.AuthenticationMod

Owin中间件实现在添加质询之前查找自己的身份验证类型,因此只有适当的中间件响应。可以同时使用多个挑战

protected override Task ApplyResponseChallengeAsync()
{
    if (Response.StatusCode == 401)
    {
        var challenge = Helper.LookupChallenge(Options.AuthenticationType, Options.AuthenticationMode);

        if (challenge != null)
        {
            Response.Headers.AppendValues("WWW-Authenticate", _challenge);
        }
    }

    return Task.FromResult<object>(null);
}

但是我想为所有控制器添加一个全局配置。

您可以使用
AuthenticationManager.Challenge()
方法设置
AuthenticationResponseChallenge
。例如,在startup.cs中,您可以有类似于
context.Authentication.Challenge(new AuthenticationProperties(),Options.AuthenticationType)
,以便与Options.AuthenticationType对应的中间件在查找时返回此质询


活动中间件将尝试处理所有传出的挑战,而不考虑其AuthenticationType。通常,只有cookie中间件被设置为主动,而所有其他中间件都是被动的。对于要对质询进行操作的被动中间件,质询应具有匹配的AuthenticationType。

因此,当我在Startup.cs中将质询添加到AuthenticationManager时,
Microsoft.Owin.Security.Infrastructure.SecurityHelper.LookupChallenge将在我的中间件中返回质询,对吗?如何在Startup.cs中访问AuthenticationManager?我找不到名称空间/类。
context.Authentication.Challenge()
。上下文类型为HttpContext,应在startup.cs或控制器中可用。身份验证属性的类型为AuthenticationManager。正在尝试Startup.cs中的HttpContext.Current.GetOwinContext().Authentication.Challenge(“Basic”)
,会抛出“上下文中找不到owin.Environment项”。这并不让我感到意外,因为Startup.cs在请求上下文之外运行。
Request.GetOwinContext().Authentication.Challenge("Basic");