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

时间:2018-12-20 06:19:29

标签: c# azure google-authentication

我正在使用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

我收到以下错误消息和堆栈跟踪 enter image description here

  

[WebException:远程服务器返回错误:(400)错误   Request。] 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   控制器,对象[]参数)+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

1 个答案:

答案 0 :(得分:1)

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

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

希望有帮助!