[ Google login works on localhost and test server, but not azure website (app service) ]
I am having a problem with signing in users with google on an Azure website using OWIN. Everything works fine localhost and on our test server, but when I deploy to our Azure website, the login fails.
I am using web api and OWIN to handle the authentication, and I have narrowed it down to this "simple" problem:
await AuthenticationManager.GetExternalLoginInfoAsync();
is returning null when deployed to azure.
- I have checked and double-checked the return url in the google api manager.
- I have tried setting a dummy session and clearing sessions as mentioned here: OWIN OpenID provider - GetExternalLoginInfo() returns null
- I write Googles clientID and clientSecret out in a log, so I know they are correct
Has anyone had similar problems when deploying to azure?
UPDATE:
Here is the code flow: First we hit the event "OnAuthenticated" on our google provider in Startup.Auth.cs when a user is logging in:
public void ConfigureAuth(IAppBuilder app)
{
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.UseKentorOwinCookieSaver();
var provider = new CookieAuthenticationProvider();
var originalHandler = provider.OnApplyRedirect;
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)),
OnApplyRedirect = context =>
{
if (!context.Request.Uri.LocalPath.StartsWith(VirtualPathUtility.ToAbsolute("~/api")))
{
context.RedirectUri = new Uri(context.RedirectUri).PathAndQuery;
originalHandler.Invoke(context);
}
}
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
var options = new GoogleOAuth2AuthenticationOptions
{
ClientId = System.Configuration.ConfigurationManager.AppSettings["googleServiceAccountWebApplicationClientId"],
ClientSecret = System.Configuration.ConfigurationManager.AppSettings["googleServiceAccountWebApplicationClientSecret"],
Provider = new GoogleOAuth2AuthenticationProvider
{
OnAuthenticated = context =>
{
//....
return Task.FromResult(0);
}
},
AccessType = "offline",
SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie
};
app.UseGoogleAuthentication(options);
}
After this event, we hit the method "ExternalLoginCallback" and the first thing we do is to call await AuthenticationManager.GetExternalLoginInfoAsync(); which returns null
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
//loginInfo is always null when published to Azure website
if (loginInfo == null)
{
return RedirectToAction("NoAccess", "Login");
}
//....
}
Answer 1
I finally found the problem. It was an unhandled exception in the "OnAuthenticated" event. It took some time to find the problems, because this line:
HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
Sometimes failed on azure. I changed alot of code to try and find/fix this problem, but I do believe it was an exception in OnAuthenticated.