部署到 Azure 时,Google 身份验证不起作用

Posted

技术标签:

【中文标题】部署到 Azure 时,Google 身份验证不起作用【英文标题】:Google authentication not working when deployed to azure 【发布时间】:2019-05-20 15:54:31 【问题描述】:

我正在使用 Google 进行身份验证。下面的代码在我本地运行时可以正常工作,而当我将其部署到 azure 时会引发错误:

 HttpWebRequest webRequest = 
(HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
                    webRequest.Method = "POST";
                    Parameters = "code=" + code + "&client_id=" + googleplus_client_id + "&client_secret=" + googleplus_client_secret + "&redirect_uri=" + googleplus_redirect_url + "&grant_type=authorization_code";
                    byte[] byteArray = Encoding.UTF8.GetBytes(Parameters);
                    webRequest.ContentType = "application/x-www-form-urlencoded";
                    webRequest.ContentLength = byteArray.Length;
                    Stream postStream = webRequest.GetRequestStream();
                    postStream.Write(byteArray, 0, byteArray.Length);
                    postStream.Close();
                    WebResponse response = webRequest.GetResponse();//**Error Occurs here**

我也试过

var values = new NameValueCollection();
var resp = client.UploadValues("https://accounts.google.com/o/oauth2/token?" + Parameters, values );
var responseString = Encoding.Default.GetString(resp);
serStatus = JsonConvert.DeserializeObject<GooglePlusAccessToken>(responseString);

这再次在本地工作,但是当我将它部署到天蓝色时它不起作用

重定向网址如下:

https://sample.com/Home/User?code=4/uQB37PKUFlID8dSLFFTsYLEDdHWWOprWaMtpa6YasdxxxxxxxxxxH8OQVxg3UZMoDc&scope=email%20profile%20https://www.googleapis.com/auth/userinfo.profile%20https://www.googleapis.com/auth/userinfo.email

我收到以下错误消息和堆栈跟踪

[WebException: 远程服务器返回错误: (400) Bad 请求。] System.Net.HttpWebRequest.GetResponse() +1399 SomeSln.Controllers.HomeController.User() 在 C:\CodePath\Controllers\HomeController.cs:63 lambda_method(闭包 , ControllerBase , Object[] ) +62 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器,Object[] 参数)+14 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary2 参数)+169 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary`2 参数)+27 System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22 System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32 System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d() +50 System.Web.Mvc.Async.c__DisplayClass46.b__3f() +228 System.Web.Mvc.Async.c__DisplayClass33.b__32(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34

【问题讨论】:

能否请您包含完整的异常堆栈 当然,我已经添加了 有没有机会发布原始字符串值而不是发布图像?阅读起来很痛苦,我认为它是 SO 中的首选。谢谢 另外请检查从远程服务器返回的400是否包含消息/正文 @JohnB 完成约翰 【参考方案1】:

这可能与重定向 URL 不在 Google 端的批准列表中有关,而在本地运行时使用的 localhost 是。

查看OAuth 2.0 >> Web applications (ASP.NET MVC) 了解更多信息。

希望对你有帮助!

【讨论】:

我添加了重定向 URL 并在登录后重定向。但是在 Azure 中获取令牌部分似乎不起作用 我建议您尝试按照我发布的链接中的Authorization Code Flow 示例进行操作(从您的代码看来,您正在使用授权代码流),看看有什么不同。

以上是关于部署到 Azure 时,Google 身份验证不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Azure 应用服务中的 LDAP 身份验证失败

WCF 配置/身份验证在 Azure 中不起作用

使用身份验证托管的 Blazor Wasm 上出现 Azure 500 错误

对 ML Web 服务的基于 Azure Kubernetes 令牌的身份验证

具有 Azure 应用服务身份验证的 .NET Core 应用

Azure Google身份验证获取用户个人资料信息