简体   繁体   English

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

[英]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.

相关问题 为什么从 azure 应用程序服务中的表单发布时出现 404 错误,但它在 localhost 中完美运行 - Why do i get 404 error when posting from form in azure app service but It works perfectly in localhost 网站重定向基于Windows Server IIS和较新的Azure App服务中安装的URL到URL - Website redirect based on URL to App installed in Windows Server IIS and Newer Azure App service 网站可以在localhost上运行,但不能在上载到服务器时出现错误 - Website works on localhost but not When uploaded to server it is giving an error 共享1个网站和1个Web API的Azure应用服务 - Share azure app service for 1 website and 1 web api DelegatingHandler在Localhost上有效,但在Azure上无效 - DelegatingHandler works on Localhost but not on Azure Azure App Service无法访问SQL Server-用户&#39;NT AUTHORITY \\ ANONYMOUS LOGON&#39;登录失败 - Azure App Service can't access SQL Server - Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON' Facebook登录在localhost中有效,但在webhost中无效 - Facebook Login works in localhost but not in webhost 为Azure移动应用程序网站登录指定重定向URL - Specifying redirect url for Azure Mobile App website login Google API未授权测试服务器上的站点,但在localhost上工作 - Google api not authorizing for site on test server, but working on localhost Azure App Service上的服务器垃圾收集 - Server Garbage Collection on Azure App Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM