OAuth 2.0 Code Flow / IdentityServer4 不能从认证服务器成功跳转回客户端的问题

场景一:Mac ,visual studio for mac,Asp.net Core 3.1 ,Identityserver4(4.1.1) 

采用 Client Credentails、Resource Owner Password Credentails授权类型时,均可以实现远程认证。

但采用 Authorization Code 对Web app(客户端)授权时,客户端可以顺利跳转到Identityserver4,在认证服务器上输入用户名密码登录后,并不能成功跳转回客户端。查看日志并没有报错,跟踪Identityserver4官方模板下的代码,也不能找到错误。

在Identityserver4的官方文档中也没有找到相关问题的说明,只是看到了“推荐使用IIS...”,

无奈只能转到 windows开发环境,但完成代码的相关代码转换后再次调试时,仍然是同样的问题。

在网上查了相关资料,看了相关的解决方案,最终也没有找到解决问题的办法。

决定基于OAuth 2.0自己写代码,在windows环境下,搭建了新的场景。

场景二:win10,visual studio 2019,asp.net core 3.1

仍然被卡在由认证服务端跳转回客户端上。

 /// <summary>
        /// 参数命名需要符合OAuth 2.0的规范,客户端只要遵行这个协议可以获得认证服务
        /// 响应客端:config.AuthorizationEndpoint = "https://localhost:50000/OAuth/Authorize";
        /// </summary>
        /// <param name="response_type">认证流程类型 </param>
        /// <param name="client_id"></param>
        /// <param name="redirect_uri"></param>
        /// <param name="scope">请求范围,如email\等claim</param>
        /// <param name="state">随机生成的字符串用来返回同一客户端</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Authorize(
            string response_type,
            string client_id,
            string redirect_uri,
            string scope,
            string state
            )
        {
            var query = new QueryBuilder();
            query.Add("redirectUri", redirect_uri);
            query.Add("state", state);
            return View(model:query.ToString());
        }
        /// <summary>
        /// 参数命名符合OAuth 2.0
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="redirectUri"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Authorize(
            string userName,
            string redirectUri,
            string state)
        {
            const string code = "anyStringCanBeTheCode";
            var query = new QueryBuilder();
            query.Add("code", code);
            query.Add("state", state);
            var afterLoginRedirectURL = $"{redirectUri}{query.ToString()}";//这个地址怎么也跳不回去
            return Redirect(afterLoginRedirectURL);
        }

但这时可以看到相关错误信息:

An unhandled exception occurred while processing the request.
Exception: Correlation failed.
Unknown location

Exception: An error was encountered while handling the remote login.
Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler<TOptions>.HandleRequestAsync()

Stack Query Cookies Headers Routing
Exception: Correlation failed.

Show raw exception details
Exception: An error was encountered while handling the remote login.
Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler<TOptions>.HandleRequestAsync()
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Show raw exception details

报出错误来了,相信可以很快解决。

 

posted @ 2020-11-03 16:49  cadreman  阅读(391)  评论(1)    收藏  举报