2016-08-24 1 views
0

J'utilise Google OAuth comme mode d'authentification pour mon application Web .NET. Bien qu'il semble fonctionner correctement sur ma machine, sur l'environnement en direct, il semble fonctionner de façon intermittente.L'authentification Google fonctionne par intermittence

Voici les informations saisies dans la console développeur:

Google Developer Console

et l'API Google+ est configuré pour être activé:

Google+

La méthode par défaut ExternalLogin est la suivante :

public ActionResult ExternalLogin(string provider, string returnUrl) 
{ 
    return new ChallengeResult(provider, 
     Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl })); 
} 

Alors que le ExternalLoginCallback est défini comme suit:

public async Task<ActionResult> ExternalLoginCallback(string returnUrl) 
{ 
    var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); 
    if (loginInfo == null) 
    { 
     return RedirectToAction("Login"); 
    } 

    var userEmail = loginInfo.Email; 
    var loggedInUser = VerifyAndAuthenticateUser(userEmail); 
    if (loggedInUser != null) 
    { 
     FormsAuthentication.SetAuthCookie(userEmail, false); 
     return RedirectToLocal(returnUrl); 
    } 

    return RedirectToAction("login", "account"); 
} 

Et le fournisseur Google Id et secret sont remplis dans le fichier Startup.Auth.cs:

app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions() 
{ 
    ClientId = "xxxx", 
    ClientSecret = "xxxx" 
}); 

Mon web.config contient l'authentification des formulaires dans l'élément system.web : J'ai besoin de quelques lignes de code dans le ExternalLogin et ExternalLoginCallback méthodes pour se connecter où il échoue, et il semble que le ExternalLoginCallback n'a pas pu être appelé. Encore une fois, cela se produit par intermittence car parfois je suis capable de compléter mon login. Quel pourrait être le problème?

Répondre

0

Le problème a été résolu en appliquant les deux modifications par rapport à la référence SO réponses suivantes:

Variation Startup.Auth (OWIN's GetExternalLoginInfoAsync Always Returns null)

var google = new GoogleOAuth2AuthenticationOptions 
{ 
    ClientId = "ClientId", 
    ClientSecret = "ClientSecret", 
    Provider = new GoogleOAuth2AuthenticationProvider() 
}; 
google.Scope.Add("email"); 
app.UseGoogleAuthentication(google); 

Variation AccountController (MVC5 Null Reference with facebook login)

public ActionResult ExternalLogin(string provider, string returnUrl) 
{ 
    ControllerContext.HttpContext.Session.RemoveAll(); 
    var redirectUri = Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }); 
    return new ChallengeResult(provider, redirectUri); 
}