0

so i use google authentication to validate the users on our system here is my code

public IActionResult Login()
{
    System.IO.File.WriteAllText("log.txt", Url.Action("ExternalLoginCallback"));

    return new ChallengeResult(
          GoogleDefaults.AuthenticationScheme,
          new AuthenticationProperties
          {
              RedirectUri = Url.Action("ExternalLoginCallback")
    });
}

public IActionResult ExternalLoginCallback()
        {
            System.IO.File.AppendAllText("log.txt", "I am redirected");

            var authenticateResult = HttpContext.AuthenticateAsync("External").Result;

            if (!authenticateResult.Succeeded)
                return BadRequest(); // TODO: Handle this better.

            var claimsIdentity = new ClaimsIdentity("Application");

            claimsIdentity.AddClaim(authenticateResult.Principal.FindFirst(ClaimTypes.NameIdentifier));
            claimsIdentity.AddClaim(authenticateResult.Principal.FindFirst(ClaimTypes.Email));

            var identity = authenticateResult.Principal.Identities.FirstOrDefault();

            if (identity != null)
            {
                var emailClaim = identity.Claims.FirstOrDefault(c => c.Type.ToLower().Contains("emailaddress"));
                if (emailClaim == null)
                {
                    return BadRequest();
                }

                var emailData = emailClaim.Value;
                if (string.IsNullOrEmpty(emailData))
                {
                    return BadRequest();
                }

                var connectionString = _config.GetConnectionString("DefaultConnection");

                UserBL userBL = new UserBL(connectionString);
                var userModel = userBL.GetUserData(emailData);
                if(userModel == null || userModel.HasError)
                {
                    return BadRequest();
                }

                HttpContext.Session.SetString("UserData", JsonConvert.SerializeObject(userModel.Data));

                if (userModel.Data.UserRoles.Any(r => r.Id == (int)UserRolesEnum.ProductOwner
                        || r.Id == (int)UserRolesEnum.CopyEditor))
                {
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    return RedirectToAction("List", "Translation");
                }

                return RedirectToAction("UnAuthorized");
            }

            return BadRequest();
        }


  

it worked fine users were redirected to google to enter their credentials and after that google redirects them back to the website till couple of days back google stopped redirecting back to the website and stays stuck on https://accounts.google.com.eg/accounts/SetSID

any pointers to how i can debug this or how to solve it

any help would be appreciated

a.tolba
  • 137
  • 1
  • 1
  • 13
  • Try to use F12 developer tools to check if there has any error message? And, perhaps the issue is related to the cache, try to clear the browser data(cookie, cache and so on), then check whether it works or not. Besides, I also found some similar threads, it seems that the issue might be related to the session cookie is missing, you have to set a `dummy` value in the session, reference: [Link 1](https://stackoverflow.com/questions/33365322/) and [Link 2](https://stackoverflow.com/questions/22535146/), you could check them. – Zhi Lv Sep 28 '20 at 14:28
  • thank you for the reply, i tried it but still didn't solve the issue – a.tolba Sep 29 '20 at 23:54
  • is there any way i could debug this ? the developer tools does not show anything is there any option in google developer console that could log any errors about this ? – a.tolba Oct 01 '20 at 02:45
  • You could try to use F12 Developer Network tools to check the HTTP request whether the related Http request is successful or not? – Zhi Lv Oct 01 '20 at 08:40
  • thank you for your patience, i found the solution after checking the query string length that was sent from google, i posted the answer below – a.tolba Oct 03 '20 at 22:46

1 Answers1

0

Finally after a week i found the problem, it was not in the code it was the server.

The IIS limits the query string character count (i think the default is 260 character) and in my case google responds with a query string with 544 character in which the IIS does not accept or respond to so it stays stuck on the SetSID page

Here is where i found the solution to increase the query string max length Increase max url length in asp.net core

as of why it was working and then stopped this i could not figure it out

a.tolba
  • 137
  • 1
  • 1
  • 13