I decided to give the new Google Oauth2 middleware a try and it has pretty much broken everything. Here is my provider config from startup.auth.cs.. When turned on, all of the providers including the google provider get a 500 internal server on Challenge. However the details of the internal server error are not available and I cant figure out how to turn on any debugging or tracing for the Katana middleware. Seems to me like they were in a rush to get the google Oauth middleware out the door.
//// GOOGLE
var googleOptions = new GoogleOAuth2AuthenticationOptions
{
ClientId = "228",
ClientSecret = "k",
CallbackPath = new PathString("/users/epsignin")
SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
Provider = new GoogleOAuth2AuthenticationProvider
{
OnAuthenticated = context =>
{
foreach (var x in context.User)
{
string claimType = string.Format("urn:google:{0}", x.Key);
string claimValue = x.Value.ToString();
if (!context.Identity.HasClaim(claimType, claimValue))
context.Identity.AddClaim(new Claim(claimType, claimValue, XmlSchemaString, "Google"));
}
return Task.FromResult(0);
}
}
};
app.UseGoogleAuthentication(googleOptions);
ActionMethod Code:
[AllowAnonymous]
public ActionResult ExternalProviderSignIn(string provider, string returnUrl)
{
var ctx = Request.GetOwinContext();
ctx.Authentication.Challenge(
new AuthenticationProperties
{
RedirectUri = Url.Action("EPSignIn", new { provider })
},
provider);
return new HttpUnauthorizedResult();
}
There is no need to specify
CallbackPath
inUseGoogleAuthentication
:Just keep the Google setting for Authorized redirect
URIs
as:Owin handles signin-google internally and redirects to the redirectUri as mentioned in your code for ChallengeResult class. Which is Account/ExternalLoginCallback.
The answers given so far led me down a really dark path that I wish I had not traveled... the solution is simple make sure that the following 3 things match:
1) In the Google OATH Credentials (https://console.developers.google.com/):
2) In your
AccountController
:Notice the Action is "ExternalLoginCallback"
3) In your
App_Start\Startup.Auth.cs
Notice the
CallbackPath
again has the samePathString
as the other 2Finally, if you're still not getting it, set your authentication mode to None in your app
Web.config
to get some more details about the issue.
This took me hours to figure out, but the issue is the
CallbackPath
as mentioned by @CrazyCoder. I realised that theCallbackPath
inpublic void ConfigureAuth(IAppBuilder app)
MUST be different to when it is being set in theChallengeResult
. If they are the same a 500 error is thrown in OWIN.My code is for
ConfigureAuth(IAppBuilder app)
isMy 'callbacks' Controller code is:
It is all working now, although would love to know exactly what is happening 'under the bonnet'
My advice, unless you have another requirement, is to let OWIN use default redirect paths and make sure you don't use them yourself.
Got it working vanilla from the tutorial with ONE simple change - just posting this for any nubes to this approach. I think the problems related to oauth2 in this instance are largely fleshed out in the latest templates/apis - what I mean is, if you are starting from scratch, you may be in luck - read on:
I JUST did this tutorial https://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-deploy-aspnet-mvc-app-membership-oauth-sql-database/
and referenced this also http://blogs.msdn.com/b/webdev/archive/2014/07/02/changes-to-google-oauth-2-0-and-updates-in-google-middleware-for-3-0-0-rc-release.aspx
The one change: it worked but ONLY after enabling google+ apis in the newest version of the google developer site.
(Just go to google api lib manager, sign in and search the apis directory for the google+ api).
Note: for me the Google+ api was disabled by default.
I did nothing else unique.
Cheers
I'm using the default ASP.NET MVC 5 template with Identity Authentication for simplicity, but hopefully this can be modified for different use cases.
StartupAuth.cs
Do not customize the redirect path. It gets replaced by /signin-google anyways and my attempts at getting around that caused "silent" (not in the debugger) Internal Server 500 errors.
Make sure to add http://whatever.com/signin-google to https://console.developers.google.com/ in your
APIs & auth
>Credentials
>Redirect URIs
section.RouteConfig.cs
Add a route to a permanent redirect controller action to your routes. Permanent redirects are the only thing that will suffice here. It is not enough to simply direct directly to the Callback URL.
AccountController.cs
Permanent redirect to the built-in callback method and you should be fine.
A template project has been posted on GitHub for reference: https://github.com/Pritchard/Test-AspNetGoogleOAuth2Authentication