public async Task <AuthorizationResult> AcquireAuthorizationAsync(Uri authorizationUri, Uri redirectUri, IDictionary <string, string> additionalHeaders, CallState callState) { AuthorizationResult authorizationResult = null; StringBuilder builder = new StringBuilder(); if (additionalHeaders != null) { bool isFirst = true; foreach (var key in additionalHeaders.Keys) { if (!isFirst) { builder.Append("\r\n"); } builder.AppendFormat("{0}: {1}", key, additionalHeaders[key]); isFirst = false; } } var sendAuthorizeRequest = new Action( delegate { authorizationResult = this.Authenticate(authorizationUri, redirectUri, builder.ToString()); }); // If the thread is MTA, it cannot create or communicate with WebBrowser which is a COM control. // In this case, we have to create the browser in an STA thread via StaTaskScheduler object. if (Thread.CurrentThread.GetApartmentState() == ApartmentState.MTA) { using (var staTaskScheduler = new StaTaskScheduler(1)) { try { Task.Factory.StartNew(sendAuthorizeRequest, CancellationToken.None, TaskCreationOptions.None, staTaskScheduler).Wait(); } catch (AggregateException ae) { PlatformPlugin.Logger.Error(callState, ae.InnerException); // Any exception thrown as a result of running task will cause AggregateException to be thrown with // actual exception as inner. Exception innerException = ae.InnerExceptions[0]; // In MTA case, AggregateException is two layer deep, so checking the InnerException for that. if (innerException is AggregateException) { innerException = ((AggregateException)innerException).InnerExceptions[0]; } throw innerException; } } } else { sendAuthorizeRequest(); } return(await Task.Factory.StartNew(() => authorizationResult).ConfigureAwait(false)); }
public async Task<AuthorizationResult> AcquireAuthorizationAsync(Uri authorizationUri, Uri redirectUri, IDictionary<string, string> additionalHeaders, CallState callState) { AuthorizationResult authorizationResult = null; StringBuilder builder = new StringBuilder(); if (additionalHeaders != null) { bool isFirst = true; foreach (var key in additionalHeaders.Keys) { if (!isFirst) { builder.Append("\r\n"); } builder.AppendFormat("{0}: {1}", key, additionalHeaders[key]); isFirst = false; } } var sendAuthorizeRequest = new Action( delegate { authorizationResult = this.Authenticate(authorizationUri, redirectUri, builder.ToString()); }); // If the thread is MTA, it cannot create or communicate with WebBrowser which is a COM control. // In this case, we have to create the browser in an STA thread via StaTaskScheduler object. if (Thread.CurrentThread.GetApartmentState() == ApartmentState.MTA) { using (var staTaskScheduler = new StaTaskScheduler(1)) { try { Task.Factory.StartNew(sendAuthorizeRequest, CancellationToken.None, TaskCreationOptions.None, staTaskScheduler).Wait(); } catch (AggregateException ae) { PlatformPlugin.Logger.Error(callState, ae.InnerException); // Any exception thrown as a result of running task will cause AggregateException to be thrown with // actual exception as inner. Exception innerException = ae.InnerExceptions[0]; // In MTA case, AggregateException is two layer deep, so checking the InnerException for that. if (innerException is AggregateException) { innerException = ((AggregateException)innerException).InnerExceptions[0]; } throw innerException; } } } else { sendAuthorizeRequest(); } return await Task.Factory.StartNew(() => authorizationResult).ConfigureAwait(false); }
public async Task <AuthorizationResult> AcquireAuthorizationAsync(Uri authorizationUri, Uri redirectUri, RequestContext requestContext) { AuthorizationResult authorizationResult = null; var sendAuthorizeRequest = new Action( delegate { authorizationResult = Authenticate(authorizationUri, redirectUri); }); // If the thread is MTA, it cannot create or communicate with WebBrowser which is a COM control. // In this case, we have to create the browser in an STA thread via StaTaskScheduler object. if (Thread.CurrentThread.GetApartmentState() == ApartmentState.MTA) { using (var staTaskScheduler = new StaTaskScheduler(1)) { try { Task.Factory.StartNew(sendAuthorizeRequest, CancellationToken.None, TaskCreationOptions.None, staTaskScheduler).Wait(); } catch (AggregateException ae) { requestContext.Logger.Error(ae.InnerException); // Any exception thrown as a result of running task will cause AggregateException to be thrown with // actual exception as inner. Exception innerException = ae.InnerExceptions[0]; // In MTA case, AggregateException is two layer deep, so checking the InnerException for that. if (innerException is AggregateException) { innerException = ((AggregateException)innerException).InnerExceptions[0]; } throw innerException; } } } else { sendAuthorizeRequest(); } return(await Task.Factory.StartNew(() => authorizationResult).ConfigureAwait(false)); }