Problem generating token on C#

2829
1
05-05-2021 02:23 PM
JuanFranciscoRuano
New Contributor

Hi, I am trying to use the generateToken to acces my service. When my function tries to communicate with the server the program crash and drop me this error.

System.AggregateException was unhandled by user code
HResult=-2146233088
Message=Se han producido uno o varios errores.
Source=mscorlib
StackTrace:
en System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
en System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
en System.Threading.Tasks.Task`1.get_Result()
en Service.validacionExisteSolicitudSurveyService(String num) en d:\Users\juan.ruano\Documents\Visual Studio 2015\WebSites\xxxxxxxxxx\App_Code\Service.cs:línea 146
en SyncInvokevalidacionExisteSolicitudSurveyService(Object , Object[] , Object[] )
en System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
en System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
InnerException:
HResult=-2146233088
Message=An error occurred while sending the request.
Source=mscorlib
StackTrace:
en System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
en System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
en System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
en AuthenticationService.<AuthenticateService>d__1.MoveNext() en d:\Users\juan.ruano\Documents\Visual Studio 2015\WebSites\xxxxxxxx\App_Code\AuthenticationToken\AuthenticationService.cs:línea 77
--- Fin del seguimiento de la pila de la ubicación anterior donde se produjo la excepción ---
en System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
en System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
en System.Runtime.CompilerServices.TaskAwaiter.GetResult()
en Service.<DoWorkService>d__5.MoveNext() en d:\Users\juan.ruano\Documents\Visual Studio 2015\WebSites\xxxxxxxxxxxxxx\App_Code\Service.cs:línea 171
InnerException:
HResult=-2146233079
Message=Se ha terminado la conexión: Error inesperado de envío.
Source=System
StackTrace:
en System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
en System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
InnerException:
HResult=-2146232800
Message=No se puede escribir datos de en la conexión de transporte: Se ha forzado la interrupción de una conexión existente por el host remoto.
Source=System
StackTrace:
en System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
en System.Net.PooledStream.EndWrite(IAsyncResult asyncResult)
en System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
InnerException:
ErrorCode=10054
HResult=-2147467259
Message=Se ha forzado la interrupción de una conexión existente por el host remoto
NativeErrorCode=10054
Source=System
StackTrace:
en System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
en System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
null

also here is my code. 

public async Task AuthenticateService(int? desiredExpiration = null)
{
var tokenEndpoint =ServerUrl;

var data = new Dictionary<string, string>();
data["username"] = Usuario;
data["password"] = Password;
data["client"] = "requestedip";
var content = new FormUrlEncodedContent(data);

HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Post, "https://www.arcgis.com/sharing/rest/generateToken") { Content = content };
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Referrer = new Uri("https://arcgis.com");

var t = await httpClient.SendAsync(msg);
var s = await t.Content.ReadAsStringAsync();

}

I have been stuck on this problem and try to solve it somehow but it seems to crush when the program try to communicate with the server.

1 Reply
OSINT_ENGINEER
New Contributor III

Maybe you should take a look at Dave Timmins Anywhere.ArcGIS implementation. They solved a lot of token generations stuff using ArcGIS Online and ArcGIS Enterprise. I would start to inspect there Anywhere.ArcGIS.TokenProvider implementation.

Enjoy.