_signInManager.GetExternalLoginInfoAsync() returns Apple 登录为空

_signInManager.GetExternalLoginInfoAsync() returns null for Apple Login

我在我的 .Net Core Razor Pages 应用程序 (.Net 5) 中设置了“使用 Apple 登录”,该应用程序使用 Microsoft Identity Framework 进行用户管理。

我遵循了 Scott 的 this 教程,它帮助我到达了 Apple 登录页面。

但是在调用回调端点成功登录后,我在 _signInManager.GetExternalLoginInfoAsync() 方法调用中得到 null

我的初步研究表明 ID 令牌可能不包含所需数据。 这是正确的,因为 Apple 返回的 ID 令牌不包含 emailname,即使它是在 scope.

中请求的

示例请求:https://appleid.apple.com/auth/authorize?client_id=net.demo.client&redirect_uri=https%3A%2F%2Fdemo.website.net%2Fsignin-apple&response_type=code%20id_token&scope=email%20name&response_mode=form_post&nonce=637679-omitted

这是从 Startup.ConfigureServices() 方法调用的身份验证设置:

IdentityModelEventSource.ShowPII = true;
        services.AddAuthentication(options =>
        {
            //options.DefaultAuthenticateScheme = "cookie";//Commented because this line was causing the Google login stop.
            //options.DefaultChallengeScheme = "apple";//Commented because this line was causing the Google login stop.
        })
               .AddCookie("cookie")
               .AddOpenIdConnect("apple", "Apple", async options =>
               {
                   options.Authority = "https://appleid.apple.com"; // disco doc: https://appleid.apple.com/.well-known/openid-configuration

                   options.ResponseType = "code id_token";
                   options.SignInScheme = "cookie";

                   options.DisableTelemetry = true;

                   options.Scope.Clear(); // otherwise I had consent request issues
                   options.Scope.Add("email");
                   options.Scope.Add("name");
                   options.ClientId = "net.demo.client"; // Service ID
                   options.CallbackPath = "/signin-apple"; // corresponding to your redirect URI

                   options.Events.OnAuthorizationCodeReceived = context =>
                      {
                          context.TokenEndpointRequest.ClientSecret = TokenGenerator.CreateNewToken();
                          return Task.CompletedTask;
                      };
                   options.Events.OnRedirectToIdentityProvider = context =>
                   {
                       var builder = new UriBuilder(context.ProtocolMessage.RedirectUri);
                       builder.Scheme = "https";
                       builder.Port = -1;
                       context.ProtocolMessage.RedirectUri = builder.ToString();
                       return Task.FromResult(0);
                   };
                   options.UsePkce = false; // apple does not currently support PKCE (April 2021)
               })
           ;

这里是回调端点:

public async Task<IActionResult> OnGetCallbackAsync(string returnUrl = null, string remoteError = null)
    {
        returnUrl = returnUrl ?? Url.Content("~/");
        if (remoteError != null)
        {
            ErrorMessage = $"Error from external provider: {remoteError}";
            return RedirectToPage("./Login", new { ReturnUrl = returnUrl });
        }
        var info = await _signInManager.GetExternalLoginInfoAsync();//Returns null.
        if (info == null)
        {
            ErrorMessage = "Error loading external login information.";
            return RedirectToPage("./Login", new { ReturnUrl = returnUrl });
        }
    //Code omitted...
}

ASP.Net 身份是否配置为使用“cookie”方案进行外部登录?

默认使用IdentityConstants.ExternalScheme。尝试使用:

options.SignInScheme = IdentityConstants.ExternalScheme; 

而不是

options.SignInScheme = "cookie";