Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Google身份验证进行外部登录_C#_Asp.net Core - Fatal编程技术网

C# 使用Google身份验证进行外部登录

C# 使用Google身份验证进行外部登录,c#,asp.net-core,C#,Asp.net Core,我正在使用Asp.net内核开发带有google身份验证登录的web应用程序 首先,我成功地获得了google认证并注册了用户。但问题是,即使用户使用Google凭据注册到应用程序,它也会一次又一次地要求注册应用程序 我发现这是由于ExternalLoginInInAsync函数调用造成的,并且它总是给出false,为此,我更改了这些参数并尝试了几次 isPersistent:false/true)和bypassTwoFactor:true/false我对所有变量进行了测试。但它总是给出错误的结

我正在使用Asp.net内核开发带有google身份验证登录的web应用程序

首先,我成功地获得了google认证并注册了用户。但问题是,即使用户使用Google凭据注册到应用程序,它也会一次又一次地要求注册应用程序

我发现这是由于ExternalLoginInInAsync函数调用造成的,并且它总是给出false,为此,我更改了这些参数并尝试了几次 isPersistent:false/true)和bypassTwoFactor:true/false我对所有变量进行了测试。但它总是给出错误的结果。就像我用正常注册用户登录的谷歌认证一样。它也给了我同样的结果

   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();
            if (info == null)
            {
                ErrorMessage = "Error loading external login information.";
                return RedirectToPage("./Login", new { ReturnUrl = returnUrl });
            }
            var result = await 
               _signInManager.ExternalLoginSignInAsync(info.LoginProvider, 
               info.ProviderKey, isPersistent:false, bypassTwoFactor : true);

            if (result.Succeeded)
            {
                _logger.LogInformation("{Name} logged in with {LoginProvider} 
                provider.", info.Principal.Identity.Name, 
                info.LoginProvider);
                // return LocalRedirect(returnUrl);
                return LocalRedirect("/Customer/ProjectsApiKey/");
            }    

公共异步任务OnGetCallbackAsync(string returnUrl=null,string remoteError=null) { returnUrl=returnUrl??Url.Content(“~/”); if(remoteError!=null) { ErrorMessage=$”来自外部提供程序的错误: {remoteError}”; 返回重定向网页(“./Login”,新的{ReturnUrl=ReturnUrl}); } var info=await _signInManager.getexternallogininfosync(); if(info==null) { ErrorMessage=“加载外部登录信息时出错。”; 返回重定向网页(“./Login”,新的{ReturnUrl=ReturnUrl}); } var结果=等待 _signInManager.ExternalLoginInSignInAsync(info.LoginProvider, info.ProviderKey,isPersistent:false,bypassTwoFactor:true); if(result.successed) { _logger.LogInformation(“{Name}使用{LoginProvider}登录” 提供者“”,info.Principal.Identity.Name, 信息登录提供商); //返回LocalRedirect(returnUrl); 返回LocalRedirect(“/Customer/ProjectsApiKey/”); }
您能请任何已经解决此问题的人帮助我吗?我希望我应该如何检查已注册或未使用Google身份验证的用户

当GetExternalLoginFoasync返回false时,您可以检查用户是否存在,如果用户存在,则添加登录名

否则,用户没有帐户,然后要求用户创建帐户

    var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false, bypassTwoFactor: true);
    if (result.Succeeded)
    {
        return RedirectToAction("Index", "Home");
    }
    if (result.IsLockedOut)
    {
        return RedirectToAction(nameof(Lockout));
    }
    else
    {
        var user = await _userManager.FindByEmailAsync(email);
        if (user != null)
        {
            var resultTemp = await _userManager.AddLoginAsync(user, info);
            if (resultTemp.Succeeded)
            {
                await _signInManager.SignInAsync(user, isPersistent: true);
                return RedirectToAction("Index", "Home");
            }
        }
        // User does not have an account, then ask the user to create an account.
        ViewData["ReturnUrl"] = returnUrl;
        ViewData["LoginProvider"] = info.LoginProvider;
        var email = info.Principal.FindFirstValue(ClaimTypes.Email);
        return View("ExternalLogin", new ExternalLoginViewModel { Email = email });
    }

当GetExternalLoginInfoAsync返回false时,可以检查用户是否存在,如果用户存在,则添加登录名

否则,用户没有帐户,然后要求用户创建帐户

    var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false, bypassTwoFactor: true);
    if (result.Succeeded)
    {
        return RedirectToAction("Index", "Home");
    }
    if (result.IsLockedOut)
    {
        return RedirectToAction(nameof(Lockout));
    }
    else
    {
        var user = await _userManager.FindByEmailAsync(email);
        if (user != null)
        {
            var resultTemp = await _userManager.AddLoginAsync(user, info);
            if (resultTemp.Succeeded)
            {
                await _signInManager.SignInAsync(user, isPersistent: true);
                return RedirectToAction("Index", "Home");
            }
        }
        // User does not have an account, then ask the user to create an account.
        ViewData["ReturnUrl"] = returnUrl;
        ViewData["LoginProvider"] = info.LoginProvider;
        var email = info.Principal.FindFirstValue(ClaimTypes.Email);
        return View("ExternalLogin", new ExternalLoginViewModel { Email = email });
    }

我终于发现了错误,这是由于电子邮件确认的事情,在我的startup.cs类中,我添加了

 config.SignIn.RequireConfirmedEmail = true;


也非常感谢您之前的回答。

我终于找到了故障,它是由于电子邮件确认的事情,在我的startup.cs课程中,我添加了

 config.SignIn.RequireConfirmedEmail = true;


同时也非常感谢您之前的回答。

非常感谢Gökten Karadağ我会试试这个。非常感谢Gökten Karadağ我会试试这个
public async Task<IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();
            if (info == null)
            {
                ErrorMessage = "Error loading external login information during confirmation.";
                return RedirectToPage("./Login", new { ReturnUrl = returnUrl });
            }

            if (ModelState.IsValid)
            {
                // var user = new IdentityUser { UserName = Input.Email, Email = Input.Email };   comment dulith
                var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email ,EmailConfirmed=true};
                var result = await _userManager.CreateAsync(user);
                await _userManager.AddToRoleAsync(user, SD.User);
                if (result.Succeeded)
                {

                    result = await _userManager.AddLoginAsync(user, info);
                    if (result.Succeeded)
                    {
                        await _signInManager.SignInAsync(user, isPersistent: false);
                        _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
                       // return LocalRedirect(returnUrl);
                        return LocalRedirect("/Customer/ProjectsApiKey/");
                    }
                }

result.Succeeded is true