.net core 3.1 Google SSO 回调 url 未命中

.net core 3.1 Google SSO Callback url not hit

我按照 link 实现了 google SSO github.com/aspnet/Security/issues/1370。但即使在成功登录后,我也会重定向身份验证中提到的 uri 属性。它没有接受回调 url。有人可以帮忙吗?我们的应用程序是 .net core 3.1IdentityServer4. 我期待 signinoauth2 API 在 google 登录后被击中,但那没有发生。

我可以看到来自浏览器的网络调用格式如下,并出现关联错误。 https://localhost:44368/signinoauth2?state=&code=&scope=***&prompt=none

Exception: Correlation failed. Show raw exception details Exception: An error was encountered while handling the remote login. Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler.HandleRequestAsync() Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) Soulbook.Api.Startup+<>c+<b__5_1>d.MoveNext() in Startup.cs await next.Invoke(); Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) PFB my code for reference,

        [HttpGet]
        [Authorize(AuthenticationSchemes = GoogleDefaults.AuthenticationScheme)]
        [Route("/Feed")]
        public ActionResult Feed() 
        {
            return Ok();
        }
        [HttpGet]
        [Route("/signin")]
        public ActionResult SignIn()
        {
            var authProperties = new AuthenticationProperties
            {
                RedirectUri = "/"
            };
            return new ChallengeResult(GoogleDefaults.AuthenticationScheme, authProperties);
        }
        [HttpPost]
        [Route("/signinoauth2")]
        public ActionResult<LoginResponse> signinoauth2Async([FromForm]object data)
        {

            return Ok();
        }

Startup.cs

using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authentication.Google;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Authorization;


services.AddAuthentication(options =>
                    {
                        options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                        options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
                    })
                      .AddCookie(o => {
                          o.LoginPath = "/signin";
                          o.LogoutPath = "/signout";
                          o.ExpireTimeSpan = TimeSpan.FromDays(7);
                      })
                      .AddGoogle(o => {
                          o.ClientId = "***";
                          o.ClientSecret = "**";
                          o.SaveTokens = true;
                          o.CallbackPath = "/signinoauth2";
                      });

services.AddMvc(config =>
            {
                var policy = new AuthorizationPolicyBuilder()
                                .RequireAuthenticatedUser()
                                .AddAuthenticationSchemes(GoogleDefaults.AuthenticationScheme)
                                .Build();
                config.Filters.Add(new AuthorizeFilter(policy));
            }).AddNewtonsoftJson();



EDIT: Having signinoauth2 in any one of the below formats also doesnt help.
        [HttpGet]
        [Route("/signinoauth2")]
        public ActionResult<LoginResponse> signinoauth2Async(string state, string code, string scope, string prompt)
        {

            return Ok();
        }

        [HttpPost]
        [Route("/signinoauth2")]
        public ActionResult<LoginResponse> signinoauth2Async(string state, string code, string scope, string prompt)
        {

            return Ok();
        }

听起来您实际上并没有经过正确的身份验证,如果您是,应用程序将重定向到我认为其控制器具有 [Authorize] 属性的登录页面。您是否可能忘记将自己添加为身份服务器正在引用的数据库中的用户?

我假设您想在您的 enpoint 中获取 Google 用户信息? 然后您要做的就是配置外部身份验证属性。多亏了这一点,您将能够让用户进入您的重定向端点。

    [HttpGet("login/google/")]
    [AllowAnonymous]
    public async Task<IActionResult> LoginGoogle()
    {
        var properties = _signInManager.ConfigureExternalAuthenticationProperties(GoogleDefaults.AuthenticationScheme, "/api/identity/google-redirect");
        return Challenge(properties, GoogleDefaults.AuthenticationScheme);
    }

您在启动时配置的是一个回调路由,它由中间件处理并且永远不会到达控制器中的端点。你想要实现的是让用户像这样重定向路由

    [HttpGet("google-redirect")]
    [AllowAnonymous]
    public async Task<IActionResult> CallbackGoogle()
    {
        var info = await _signInManager.GetExternalLoginInfoAsync();
        return Ok();
    }