Im trying to Use GoogleAuthenticationExtensions from Microsoft.Owin.Security.Google
package inside MVC5 program. This is my StartUp.cs :
var googlePlusOptions = new GoogleOAuth2AuthenticationOptions {};
googlePlusOptions.ClientId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com";
googlePlusOptions.AuthorizationEndpoint = "https://accounts.google.com/o/oauth2/auth";
googlePlusOptions.TokenEndpoint = "https://oauth2.googleapis.com/token";
googlePlusOptions.ClientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
googlePlusOptions.CallbackPath = new PathString("/GoogleLoginCallback");
app.UseGoogleAuthentication(googlePlusOptions);
And SignInMethod method inside LoginController :
[HttpGet]
[AllowAnonymous]
public void SignInGoogle(string ReturnUrl = "/", string type = "")
{
if (!Request.IsAuthenticated)
{
if (type == "Google")
{
var owinContext = HttpContext.GetOwinContext();
owinContext.Authentication.Challenge(new AuthenticationProperties { RedirectUri = "Login/GoogleLoginCallback" }, "Google");
Response.StatusCode = 401;
Response.End();
}
}
}
And CallBack Url in same controller :
[AllowAnonymous]
public ActionResult GoogleLoginCallback()
{
var claimsPrincipal = HttpContext.User.Identity as ClaimsIdentity;
var loginInfo = GoogleLoginViewModel.GetLoginInfo(claimsPrincipal);
if (loginInfo == null)
{
return RedirectToAction("Index");
}
var user = db.UserAccounts.FirstOrDefault(x => x.Email == loginInfo.emailaddress);
if (user == null)
{
user = new UserAccount
{
Email = loginInfo.emailaddress,
GivenName = loginInfo.givenname,
Identifier = loginInfo.nameidentifier,
Name = loginInfo.name,
SurName = loginInfo.surname,
IsActive = true
};
db.UserAccounts.Add(user);
db.SaveChanges();
}
var ident = new ClaimsIdentity(
new[] {
// adding following 2 claim just for supporting default antiforgery provider
new Claim(ClaimTypes.NameIdentifier, user.Email),
new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"),
new Claim(ClaimTypes.Name, user.Name),
new Claim(ClaimTypes.Email, user.Email),
// optionally you could add roles if any
new Claim(ClaimTypes.Role, "User")
},
CookieAuthenticationDefaults.AuthenticationType);
HttpContext.GetOwinContext().Authentication.SignIn(
new AuthenticationProperties { IsPersistent = false }, ident);
return Redirect("~/");
}
Now Program goes into Login Screen Of Google but when it came back into Call Back Url the
loginInfo is null. This is the response from Call Back Url :
Things ive done so far without any result : 1-Activating Google+ APi 2-Updating the nuget to latest version (4.2 atm) 3-Add Email to TestUser or change project inside google console to production 4-Add and fill consent part in google console 5-set the js callback to blank
one thing that is kinda strange is that , if i modify ClientId the login screen of google will stop me but if i change secretId nothing will happen and still i see the error above. Ive got both of them (ClientId,SecretId) today from OAuth console panel today.
