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();
}