【发布时间】:2016-03-31 13:49:07
【问题描述】:
我们已经使用 IndentityServer 和 Owin 自托管 Web API 实现了 OAuth。每当客户端尝试访问我们经过身份验证的端点时,我们都会使用 Microsoft.Owin.Security.OpenIdConnect 中间件来拦截调用并将其重定向到 IndentityServer 进行身份验证。在 API 调用的情况下,我们不希望返回 302,而是返回带有位置标头的 401,其中包含 IdentityServer 的 url。我们可以通过设置让 OWIN 中间件返回 401
AuthenticationMode = AuthenticationMode.Passive
但我们无法添加位置标头。我们如何做到这一点?我们已经尝试设置标题(参见下面的代码),但它不起作用。似乎响应是由中间件在内部构建的。
appBuilder.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = idSrvUri.ToString(),
AuthenticationType = WebServiceConstants.OAuthAuthType,
ClientId = "beocloud",
Scope = "openid profile roles",
ResponseType = "id_token token",
AuthenticationMode = AuthenticationMode.Passive,
SignInAsAuthenticationType = WebServiceConstants.OAuthAuthType,
UseTokenLifetime = false,
Notifications = new OpenIdConnectAuthenticationNotifications
{
RedirectToIdentityProvider = n =>
{
if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.AuthenticationRequest)
{
n.ProtocolMessage.RedirectUri = n.Request.Scheme + "://" + n.Request.Host + "/";
n.Response.Headers.Add("Location", new []{n.ProtocolMessage.CreateAuthenticationRequestUrl()});
}
if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest)
{
var idTokenHint = n.OwinContext.Authentication.User.FindFirst("id_token");
if (idTokenHint != null)
{
n.ProtocolMessage.IdTokenHint = idTokenHint.Value;
}
}
return Task.FromResult(0);
}
}
});
}
【问题讨论】:
标签: c# http owin middleware identityserver3