繁体   English   中英

Google登录适用于localhost和测试服务器,但不适用于azure网站(应用服务)

[英]Google login works on localhost and test server, but not azure website (app service)

我在使用OWIN在Azure网站上使用Google登录用户时遇到问题。 一切都运行良好localhost和我们的测试服务器,但当我部署到我们的Azure网站时,登录失败。

我正在使用web api和OWIN来处理身份验证,我已将其缩小到这个“简单”问题:

await AuthenticationManager.GetExternalLoginInfoAsync(); 

部署到azure时返回null。

在部署到azure时有没有人遇到过类似的问题?

更新:

以下是代码流:首先,当用户登录时,我们在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);
}

在这个事件之后,我们点击方法“ExternalLoginCallback”,我们做的第一件事就是调用await AuthenticationManager.GetExternalLoginInfoAsync(); 返回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");
    }
    //....
}

我终于找到了问题。 这是“OnAuthenticated”活动中未经处理的例外。 花了一些时间才找到问题,因为这一行:

HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();

有时在天蓝色上失败。 我改变了很多代码来尝试找到/解决这个问题,但我确实认为这是OnAuthenticated中的一个例外。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM