我正在使用ASP.NET MVC 5开发一个企业系统。我有一个带有验证码的Registration
操作,它由来自NuGet的RecaptchaNet
库提供支持,与Google Recaptcha
一起使用。< / p>
现在,我遇到了一个问题。这是我注册行动的开始:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<JsonResult> Register(RegisterViewModel model)
{
try
{
RecaptchaVerificationHelper recaptchaHelper = this.GetRecaptchaVerificationHelper();
if (String.IsNullOrEmpty(recaptchaHelper.Response))
{
// return error
}
RecaptchaVerificationResult recaptchaResult = await recaptchaHelper.VerifyRecaptchaResponseTaskAsync();
if (recaptchaResult != RecaptchaVerificationResult.Success)
{
// return error
}
在第
行RecaptchaVerificationResult recaptchaResult = await recaptchaHelper.VerifyRecaptchaResponseTaskAsync();
我得到以下异常:
Exception text (depth 0): Unable to connect to the remote server
at Recaptcha.Web.RecaptchaVerificationHelper.<VerifyRecaptchaResponseTaskAsync>b__0()
at System.Threading.Tasks.Task1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
at BankingClient.Controllers.AccountController.<Register>d__8.MoveNext() in c:\Src\IBRC-SVN\WebClients\BankingClient\Controllers\AccountController.cs:line 152
Exception text (depth 1): No connection could be made because the target machine actively refused it 88.204.142.34:443
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
在StackOverflow找到答案后,我明白问题在于我使用代理。我已使用RecaptchaNet
DotPeek
对Resharper
库进行了反编译,以下是代码:
public Task<RecaptchaVerificationResult> VerifyRecaptchaResponseTaskAsync()
{
// some code
byte[] bytes = Encoding.ASCII.GetBytes(string.Format("privatekey={0}&remoteip={1}&challenge={2}&response={3}", (object) RecaptchaKeyHelper.ParseKey(this.PrivateKey), (object) this.UserHostAddress, (object) this._Challenge, (object) this.Response));
Uri requestUri = this.UseSsl ? new Uri("https://www.google.com/recaptcha/api/verify", UriKind.Absolute) : new Uri("http://www.google.com/recaptcha/api/verify", UriKind.Absolute);
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri);
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.ContentLength = (long)bytes.Length;
httpWebRequest.Method = "POST";
IWebProxy systemWebProxy = WebRequest.GetSystemWebProxy();
systemWebProxy.Credentials = CredentialCache.DefaultCredentials;
httpWebRequest.Proxy = systemWebProxy;
httpWebRequest.GetRequestStream().Write(bytes, 0, bytes.Length);
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
string[] strArray = (string[])null;
using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
strArray = streamReader.ReadToEnd().Split('\n');
现在,我将这段代码复制到一个空的控制台应用程序中我自己的类:
byte[] bytes = Encoding.ASCII.GetBytes("check");
Uri requestUri = new Uri("https://www.google.com/recaptcha/api/verify", UriKind.Absolute);
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri);
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.ContentLength = (long)bytes.Length;
httpWebRequest.Method = "POST";
IWebProxy systemWebProxy = WebRequest.GetSystemWebProxy();
systemWebProxy.Credentials = CredentialCache.DefaultCredentials;
httpWebRequest.Proxy = systemWebProxy;
httpWebRequest.GetRequestStream().Write(bytes, 0, bytes.Length);
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
string[] strArray = (string[])null;
using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
strArray = streamReader.ReadToEnd().Split('\n');
它完美无缺。我花了很多时间试图了解出了什么问题。如果它可能会影响某些东西,我们在组织中的任何地方都有一个ActiveDirectory身份验证,包括代理身份验证。