简体   繁体   English

部署到天蓝色的Google身份验证不起作用

[英]Google authentication not working when deployed to azure

I am authenticating using Google. 我正在使用Google进行身份验证。 The below code works fine when I run it locally whereas it throws an error when I deploy it to azure: 下面的代码在本地运行时工作正常,而在将其部署到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**

I also tried 我也试过

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);

And again this worked locally, but when I deployed it to azure it didn't work 再次在本地工作,但是当我将其部署为天蓝色时,它不起作用

The redirect url is like below: 重定向网址如下所示:

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

I get the below error message and stack trace 我收到以下错误消息和堆栈跟踪 在此处输入图片说明

[WebException: The remote server returned an error: (400) Bad Request.] System.Net.HttpWebRequest.GetResponse() +1399 [WebException:远程服务器返回错误:(400)错误的请求。] System.Net.HttpWebRequest.GetResponse()+1399
SomeSln.Controllers.HomeController.User() in C:\\CodePath\\Controllers\\HomeController.cs:63 lambda_method(Closure , ControllerBase , Object[] ) +62 C:\\ CodePath \\ Controllers \\ HomeController.cs中的SomeSln.Controllers.HomeController.User():63 lambda_method(Closure,ControllerBase,Object [])+62
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,Object []参数)+14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +169 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary2参数)+169
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27 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.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult,ActionInvocation innerInvokeState)+22
System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult)+29
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Async.WrappedAsyncResultBase1.End()+49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32 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.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.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult)+10
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Async.WrappedAsyncResultBase1.End()+49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)+34

It might be related to the redirect URL not being in the approved list at the Google side, whereas the localhost used when you run locally is. 它可能与重定向URL不在Google方面的批准列表中有关,而在localhost运行时使用的localhost则与重定向URL有关。

Take a look at OAuth 2.0 >> Web applications (ASP.NET MVC) for more information. 有关更多信息,请查看OAuth 2.0 >> Web应用程序(ASP.NET MVC)

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 部署MVC站点时Windows身份验证不起作用 - Windows authentication not working when deployed MVC site Web api在本地工作,但在部署到Azure时则不能 - Web api working locally but not when deployed to Azure 部署到Azure ASP MVC 4时捆绑不起作用 - Bundling not working when deployed to azure asp mvc 4 在Azure下部署时wsDualHttpBinding不起作用 - wsDualHttpBinding Not Working When Deployed Under Azure 部署到Azure网站时WebApi不工作 - WebApi Not Working When Deployed to Azure Website 在IIS上部署时,ASP.NET 4.5身份验证重定向不起作用 - ASP.NET 4.5 Authentication Redirect Not Working When Deployed on IIS Azure Function 事件中心 Output 绑定在部署时不起作用 - Azure Function Event Hub Output Binding not working when deployed Azure AD图形api在本地工作但在部署时失败 - Azure AD graph api working on local but fails when deployed Identity Server 4部署到Azure网站时无法正常工作 - Identity Server 4 not working when deployed to Azure Web Site 尽管在本地主机上工作,但在部署到Azure时,Azure Active Directory始终会重定向到“〜/ .auth / login / done” - Azure Active Directory always redirects to '~/.auth/login/done' when deployed to Azure despite working on localhost
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM