
[英]Manual set cookies using accesstoken/AuthenticationResult -like UseCookieAuthentication() owin middleware
[英]Set challenge in Owin middleware
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);
}
使用内置Cookie或Bearer中间件时,“Bearer”类型始终存在并被查找。
我会在哪里全局添加自己的挑战类型,以便查找? 这可以通过调用在请求上下文中手动完成
Request.GetOwinContext().Authentication.Challenge("Basic");
但我想为所有控制器添加全局配置。
您可以使用AuthenticationManager.Challenge()
方法设置AuthenticationResponseChallenge
。 例如,在startup.cs中,您可以使用context.Authentication.Challenge(new AuthenticationProperties(), Options.AuthenticationType)
,以便在查找时与Options.AuthenticationType对应的中间件恢复此挑战。
无论其AuthenticationType如何,活动中间件都将尝试处理所有传出的挑战。 通常,只有cookie中间件设置为活动,所有其他中间件都是被动的。 对于要对挑战采取行动的被动中间件,挑战应该具有匹配的AuthenticationType。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.