当我使用signinManager.GetExternalLoginInfoAsync()时,我得到的信息为空

问题描述 投票:0回答:1

我正在尝试使用身份外部提供商在 asp.net core web api 中实现使用 google 功能的登录。一切都很顺利,但是当我的网址重定向到这个外部身份验证回调函数时,我得到的信息为空。这是外部验证回调函数。

公共异步任务ExternalLoginCallback([FromQuery] string returnUrl) {

        var info = await _signInManager.GetExternalLoginInfoAsync();
        if (info != null)
        {
            var signInResult = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider,
          info.ProviderKey, isPersistent: false, bypassTwoFactor: true);
            return new MessageViewModel()
            {
                IsSuccess = signInResult.Succeeded,
                Message = "User with this account is already in table",
            };
        }
        else
        {
            var email = info.Principal.FindFirstValue(ClaimTypes.Email);
            var user = await _userManager.FindByEmailAsync(email);
            if (user == null)
            {
                user = new Users
                {
                    UserName = info.Principal.FindFirstValue(ClaimTypes.Email),
                    Email = info.Principal.FindFirstValue(ClaimTypes.Email),
                    FirstName = info.Principal.FindFirstValue(ClaimTypes.GivenName),
                    LastName = info.Principal.FindFirstValue(ClaimTypes.Surname),
                };
                await _userManager.CreateAsync(user);
            }
            await _userManager.AddLoginAsync(user, info);
            await _signInManager.SignInAsync(user, isPersistent: false);
            return new MessageViewModel()
            {
                IsSuccess = true,
                Message = "User with this account is created successfully"
            };
        }
    }

在此之前我通过这个方法返回返回url

公共异步任务ExternalLogin(字符串提供者,字符串returnUrl) { var redirectUrl = $"https://localhost:7008/api/account/external-auth-callback?returnUrl={returnUrl}"; var 属性 = _signInManager.ConfigureExternalAuthenticationProperties(provider,redirectUrl); 属性.AllowRefresh = true;

        return new LoginProviderViewModel()
        {
            Provider = provider,
            Properties = properties,
        };
    }

从我的前端,当我点击“使用谷歌登录”按钮时,会调用此方法,进入此方法后,将调用第一个 externalLogin 方法,然后我会返回返回网址,之后在前端我得到了谷歌登录页面,当我选择用户时,它转到 external-auth-callback 函数,该函数用于保存尝试使用 google 登录的用户。

const handleExternalLogin = async () => { const url =

${externalLogin}?provider=Google&returnUrl=/admin-dashboard
; const 响应 = 等待 getData(url); if (response.isSuccessfull) { const redirectUrl = response.data.properties.items[".redirect"]; const loginProvider = response.data.properties.items["LoginProvider"]; const googleAuthorizationUrl =
https://accounts.google.com/o/oauth2/v2/auth
+
?client_id=xxxxx.apps.googleusercontent.com
+
&redirect_uri=${redirectUrl}
+
&response_type=code
+
&scope=openid%20profile%20email
+
&state=${loginProvider}
; window.location.href = googleAuthorizationUrl; } };

请帮助我,因为我已经坚持了两天了。

我已经尝试了所有可能的检查,但不明白为什么信息在外部身份验证回调函数中显示为空?所以我希望有人能让我知道为什么我得到空值。

asp.net-core-identity
1个回答
0
投票

您可以尝试返回Challengeresult而不是ViewModel。这允许 ASP.NET CORE 在执行 ChallengeResult 时触发指定的身份验证方案:

[HttpGet]
[Route("ExternalLogin")]
[AllowAnonymous]
public IActionResult ExternalLogin(string provider, string returnUrl)
{

     var redirectUrl = $https://xxx/ExternalLoginCallback?returnUrl={returnUrl};
     var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
     properties.AllowRefresh = true;
     return new ChallengeResult (provider, properties);
}

当我调用方法

_signInManager.GetExternalLoginInfoAsync()
时:

© www.soinside.com 2019 - 2024. All rights reserved.