簡體   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