[英]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. 我在使用OWIN在Azure网站上使用Google登录用户时遇到问题。 Everything works fine localhost and on our test server, but when I deploy to our Azure website, the login fails. 一切都运行良好localhost和我们的测试服务器,但当我部署到我们的Azure网站时,登录失败。
I am using web api and OWIN to handle the authentication, and I have narrowed it down to this "simple" problem: 我正在使用web api和OWIN来处理身份验证,我已将其缩小到这个“简单”问题:
await AuthenticationManager.GetExternalLoginInfoAsync();
is returning null when deployed to azure. 部署到azure时返回null。
Has anyone had similar problems when deploying to azure? 在部署到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: 以下是代码流:首先,当用户登录时,我们在Startup.Auth.cs中的google提供程序上点击“OnAuthenticated”事件:
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(); 在这个事件之后,我们点击方法“ExternalLoginCallback”,我们做的第一件事就是调用await AuthenticationManager.GetExternalLoginInfoAsync(); which returns null 返回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");
}
//....
}
I finally found the problem. 我终于找到了问题。 It was an unhandled exception in the "OnAuthenticated" event. 这是“OnAuthenticated”活动中未经处理的例外。 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. 我改变了很多代码来尝试找到/解决这个问题,但我确实认为这是OnAuthenticated中的一个例外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.