【发布时间】:2020-11-12 15:05:10
【问题描述】:
我有一个框架 4.7.2 的 ASP.NET MVC 应用程序。应用程序配置为使用 OpenIDConnect 使用 IdentityServer3。当用户单击注销按钮时,将调用以下代码
操作方法首先调用注销操作方法。
[HttpPost]
public ActionResult Logout()
{
Session.Clear();
if (Request.IsAuthenticated)
{
Request.GetOwinContext().Authentication.SignOut();
}
return Redirect("/");
}
在 Owin Startup.cs 我已经配置了 OpenIDConnect。接下来会触发 RedirectToIdentityProvider 事件。
这里我设置IdTokenHint当RequestType为Logout时。
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
var cookieOptions = new CookieAuthenticationOptions
{
AuthenticationType = "Cookies",
LoginPath = new Microsoft.Owin.PathString("/Home"),
SlidingExpiration = true,
ExpireTimeSpan = GetCookieExpiration()
};
var openIdOptions = new OpenIdConnectAuthenticationOptions
{
Authority = ConfigurationManager.AppSettings["id:Authority"],
Scope = "openid email profile",
ClientId = "My ClientId",
RedirectUri = "http://localhost:58641/Home",
ResponseType = "id_token",
SignInAsAuthenticationType = "Cookies",
UseTokenLifetime = false,
Notifications = new OpenIdConnectAuthenticationNotifications
{
SecurityTokenValidated = (context) =>
{
//code here removed for brevity
return Task.FromResult(0);
},
RedirectToIdentityProvider = (context) =>
{
if (context.ProtocolMessage.RequestType == Microsoft.IdentityModel.Protocols.OpenIdConnectRequestType.LogoutRequest)
{
var idTokenHint = context.OwinContext.Authentication.User.FindFirst("id_token").Value;
context.ProtocolMessage.IdTokenHint = idTokenHint;
}
return Task.FromResult(0);
}
}
};
app.UseCookieAuthentication(cookieOptions);
app.UseOpenIdConnectAuthentication(openIdOptions);
MvcHandler.DisableMvcResponseHeader = true;
}
我看到它在打电话
/identity/connect/endsession?id_token_hint= xxxxxxxx 但是,它使用的 HTTP 动词
是OPTIONS。所以 IdentityServer 抛出错误The requested resource does not support http method 'OPTIONS'
不确定我在这里缺少什么。
编辑 1
在浏览器控制台中我看到以下错误
访问 XMLHttpRequest 在 'https://localhost:44300/identity/connect/endsession?id_token_hint=xxxxxxx' (从 'http://localhost:58641/account/logout' 重定向) “http://localhost:58641”已被 CORS 策略阻止:响应 预检请求未通过访问控制检查:否 请求中存在“Access-Control-Allow-Origin”标头 资源。
编辑 2
我有另一个具有相同注销代码的 ASP.NET 应用程序。但它发出GET 请求结束会话。
【问题讨论】:
-
您是否更改了 IdServer 中的 web.config 文件以允许使用 OPTIONS 方法?应该有一个类似的条目:
-
我认为这将是一个黑客。客户端首先不应该使用
OPTIONS动词。
标签: asp.net-mvc asp.net-identity openid-connect identityserver3 asp.net-authentication