【问题标题】:sending endsession request to identityserver throws error向身份服务器发送结束会话请求会引发错误
【发布时间】: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


【解决方案1】:

当您看到 OPTIONS 的使用并且请求包含原始标头时,这就是 CORS 预检请求。这是一个额外的安全请求,当JavaScript 客户端尝试向 API 发出 AJAX 请求。

这是为了触发 JavaScript 的结束会话吗?如果是这样,您需要在 identityServer 集中该客户端:

AllowedCorsOrigins =
{
    "https://localhost:xxxxx"
},

这是在 IdentityServer 中为每个客户端设置的。

【讨论】:

    猜你喜欢
    • 2014-01-17
    • 1970-01-01
    • 2017-05-10
    • 2013-10-24
    • 2010-11-25
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多