重定向从HTTP到HTTPS的外部登录与Owin + OAuth +谷歌
本文关键字:Owin OAuth 谷歌 登录 外部 HTTP HTTPS 重定向 | 更新日期: 2023-09-27 18:18:56
My Application Hosting使用ARR将所有页面重定向到HTTPS。
问题是它的配置方式,ASP。. Net MVC理解请求是HTTP,甚至是HTTPS。
当我检查到google认证的URL时它是这样的
&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当我这样做的解决方案。
在研究了相同问题的不同变体之后,我找到了解决方案,至少在我的特定场景中是这样。
使用负载均衡器托管在AWS EB上的MVC
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
直接导致回调数据签名问题。
在你的Google Developers Console中你配置了你的"Authorized redirect uri "。
你的URI应该是"https://[你的域名]/signin-google"如果它不是https,你的网站可能会失去从谷歌传回的凭据信息,因为你正在做一个重定向到https之前AccountController ExternalLoginCallback代码正在运行