3

I have the following code that checks to authenticate users using the google. I've tried 3 ways to do this (the 2 signin() & setting the user property). The signin seems to not be doing anything (the user property does not change at all) and even the method for setting the user property, it seems to not remember the change and I end up in an infinite redirect loop and I can't figure out why.

PS: the routing & flow of code does work, its just that when I do the redirect to "home index", authentication is not recognized and I get sent back into this logic.

FYI: The ChallengeResult class is the helper class that is generated.

    public ActionResult ExternalLogin()
    {
        // Request a redirect to the external login provider
        return new ChallengeResult("Google", Url.Action("ExternalLoginCallback", "GoogleAuth"));
    }

    [Route("ExternalLoginCallback")]
    public async Task<ActionResult> ExternalLoginCallback()
    {
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        var externalIdentity = await HttpContext.GetOwinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
        var emailClaim = externalIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email);
        var email = emailClaim.Value;
        if (loginInfo != null && (email.Contains("@mydomain.com") || email.Contains("@mydomain.ca")))
        {
            //AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            //AuthenticationManager.SignIn(externalIdentity);
            //AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, externalIdentity);

            AuthenticationManager.User = new ClaimsPrincipal(externalIdentity);

            return RedirectToAction("Index", "Home");
        }
        else
        {
            return new HttpUnauthorizedResult();
        }
    }

    private IAuthenticationManager AuthenticationManager
    {
        get
        {
            return HttpContext.GetOwinContext().Authentication;
        }
    }

In home controller:

    [Authorize]
    public ActionResult Index()
    {
        return View();
    }
user2864740
  • 60,010
  • 15
  • 145
  • 220
LostBalloon
  • 1,608
  • 3
  • 15
  • 31
  • To add some info, when `return RedirectToAction("Index", "Home");` gets called, it seems to think it is still unauthorized and brings me back to `public ActionResult ExternalLogin()` – LostBalloon Mar 31 '14 at 13:06
  • Please check the following solution http://stackoverflow.com/questions/23614000/intermittent-asp-net-oauth-issue-with-google-authenticationmanager-getexternali/23685160#23685160 – Rama Subba Reddy M May 15 '14 at 17:42

1 Answers1

1

One thing that is important to enable in the google console the Google+ API otherwise the request will return access_denied

Google Console

Once you do that if that doesn't work, then you can try to see if this answer helps you

ASP.NET Identity OWIN Middleware Google OAuth2 AuthenticationManager SignIn not working

Community
  • 1
  • 1
Oscar Marin
  • 136
  • 2
  • 8