使用Owin + OAuth + Google在ExternalLogin上从HTTP重定向到HTTPS

问题描述 投票:4回答:2

我的应用程序托管使用ARR将所有页面重定向到HTTPS。

问题是它的配置方式,ASP.Net MVC理解请求是HTTP,甚至是HTTPS。

当我检查进入谷歌身份验证的网址时,就是这样:

&redirect_uri=http%3A%2F%mydomain.com\signing-google

我正在尝试重定向到谷歌“手动”更改为HTTPS。

我试过这个:

public class ChallengeResult : HttpUnauthorizedResult
{
   ...

    public override void ExecuteResult(ControllerContext context)
    {
        var properties = new AuthenticationProperties { RedirectUri = RedirectUri };
        if (UserId != null)
            properties.Dictionary[XsrfKey] = UserId;

        var owin = context.HttpContext.GetOwinContext();

        owin.Request.Scheme = "https"; //hotfix

        owin.Authentication.Challenge(properties, LoginProvider);
    }
}

还有这个:

 app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
            {
                ClientId = Secrets.GoogleClientId,
                ClientSecret = Secrets.GoogleClientSecret,
                Provider = new GoogleOAuth2AuthenticationProvider()
                {
                    OnApplyRedirect = async context =>
                    {
                        string redirect = context.RedirectUri;

                        redirect = redirect.Replace("redirect_uri=http", "redirect_uri=https");
                        context.Response.Redirect(redirect);
                    }
                }
            });

这两种方式是令人惊讶的,谷歌可以再次重定向到我的应用程序,但是,当我尝试获取loginInfo数据为空。

 public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
    {
        if (string.IsNullOrEmpty(returnUrl))
            returnUrl = "~/";

        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        if (loginInfo == null)
        {
            //always return null, if I change from HTTP to HTTPS manually
        }

我试图看到GetExternalLoginInfoAsync()实现,但我没有找到,因为当我做这个解决方法时它总是返回null。

c# asp.net-mvc-5 owin google-authentication
2个回答
0
投票

在查看同一问题的不同变体后,我找到了解决方案,至少在我的具体情况下。

MVC使用负载均衡器托管在AWS EB上。

public void ConfigureAuth(IAppBuilder app)
{
    app.Use((ctx, next) =>
    {
        ctx.Request.Scheme = "https";
        return next();
    });

    // your other middleware configuration

    // app.UseFacebookAuthentication();
    // app.UseGoogleAuthentication();

    // other providers
}

我在所有其他配置之前放置了Use()函数,可能只需要将它放在OAuth提供程序配置之上。

我的猜测是操纵redirect_uri直接导致签署回调数据的问题。


-1
投票

在您的Google Developers Console中,您配置了“授权重定向URI”。

您的URI应为“https:// [您的域名] / signin-google”

如果不是https,您的网站可能会丢失从Google传回的凭据信息,因为您在运行AccountController ExternalLoginCallback代码之前正在重定向到https。

© www.soinside.com 2019 - 2024. All rights reserved.