diff --git a/src/Sdk/Common/Common/Authentication/FederatedCredential.cs b/src/Sdk/Common/Common/Authentication/FederatedCredential.cs
deleted file mode 100644
index 2a03c63bb..000000000
--- a/src/Sdk/Common/Common/Authentication/FederatedCredential.cs
+++ /dev/null
@@ -1,35 +0,0 @@
-using System;
-using System.Linq;
-using System.Net;
-using GitHub.Services.Common.Internal;
-
-namespace GitHub.Services.Common
-{
- ///
- /// Provides a common implementation for federated credentials.
- ///
- [Serializable]
- public abstract class FederatedCredential : IssuedTokenCredential
- {
- protected FederatedCredential(IssuedToken initialToken)
- : base(initialToken)
- {
- }
-
- public override bool IsAuthenticationChallenge(IHttpResponse webResponse)
- {
- if (webResponse == null)
- {
- return false;
- }
-
- if (webResponse.StatusCode == HttpStatusCode.Found ||
- webResponse.StatusCode == HttpStatusCode.Redirect)
- {
- return webResponse.Headers.GetValues(HttpHeaders.TfsFedAuthRealm).Any();
- }
-
- return webResponse.StatusCode == HttpStatusCode.Unauthorized;
- }
- }
-}
diff --git a/src/Sdk/Common/Common/Authentication/HttpRequestMessageWrapper.cs b/src/Sdk/Common/Common/Authentication/HttpRequestMessageWrapper.cs
deleted file mode 100644
index ea0a25313..000000000
--- a/src/Sdk/Common/Common/Authentication/HttpRequestMessageWrapper.cs
+++ /dev/null
@@ -1,66 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Net.Http;
-
-namespace GitHub.Services.Common
-{
- internal struct HttpRequestMessageWrapper : IHttpRequest, IHttpHeaders
- {
- public HttpRequestMessageWrapper(HttpRequestMessage request)
- {
- m_request = request;
- }
-
- public IHttpHeaders Headers
- {
- get
- {
- return this;
- }
- }
-
- public Uri RequestUri
- {
- get
- {
- return m_request.RequestUri;
- }
- }
-
- public IDictionary Properties
- {
- get
- {
- return m_request.Properties;
- }
- }
-
- IEnumerable IHttpHeaders.GetValues(String name)
- {
- IEnumerable values;
- if (!m_request.Headers.TryGetValues(name, out values))
- {
- values = Enumerable.Empty();
- }
- return values;
- }
-
- void IHttpHeaders.SetValue(
- String name,
- String value)
- {
- m_request.Headers.Remove(name);
- m_request.Headers.Add(name, value);
- }
-
- Boolean IHttpHeaders.TryGetValues(
- String name,
- out IEnumerable values)
- {
- return m_request.Headers.TryGetValues(name, out values);
- }
-
- private readonly HttpRequestMessage m_request;
- }
-}
diff --git a/src/Sdk/Common/Common/Authentication/HttpResponseMessageWrapper.cs b/src/Sdk/Common/Common/Authentication/HttpResponseMessageWrapper.cs
deleted file mode 100644
index 18e03b5b8..000000000
--- a/src/Sdk/Common/Common/Authentication/HttpResponseMessageWrapper.cs
+++ /dev/null
@@ -1,58 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Net;
-using System.Net.Http;
-
-namespace GitHub.Services.Common
-{
- internal struct HttpResponseMessageWrapper : IHttpResponse, IHttpHeaders
- {
- public HttpResponseMessageWrapper(HttpResponseMessage response)
- {
- m_response = response;
- }
-
- public IHttpHeaders Headers
- {
- get
- {
- return this;
- }
- }
-
- public HttpStatusCode StatusCode
- {
- get
- {
- return m_response.StatusCode;
- }
- }
-
- IEnumerable IHttpHeaders.GetValues(String name)
- {
- IEnumerable values;
- if (!m_response.Headers.TryGetValues(name, out values))
- {
- values = Enumerable.Empty();
- }
- return values;
- }
-
- void IHttpHeaders.SetValue(
- String name,
- String value)
- {
- throw new NotSupportedException();
- }
-
- Boolean IHttpHeaders.TryGetValues(
- String name,
- out IEnumerable values)
- {
- return m_response.Headers.TryGetValues(name, out values);
- }
-
- private readonly HttpResponseMessage m_response;
- }
-}
diff --git a/src/Sdk/Common/Common/Authentication/IHttpHeaders.cs b/src/Sdk/Common/Common/Authentication/IHttpHeaders.cs
deleted file mode 100644
index 8147c75fe..000000000
--- a/src/Sdk/Common/Common/Authentication/IHttpHeaders.cs
+++ /dev/null
@@ -1,14 +0,0 @@
-using System;
-using System.Collections.Generic;
-
-namespace GitHub.Services.Common
-{
- public interface IHttpHeaders
- {
- IEnumerable GetValues(String name);
-
- void SetValue(String name, String value);
-
- Boolean TryGetValues(String name, out IEnumerable values);
- }
-}
diff --git a/src/Sdk/Common/Common/Authentication/IHttpRequest.cs b/src/Sdk/Common/Common/Authentication/IHttpRequest.cs
deleted file mode 100644
index 802d7275d..000000000
--- a/src/Sdk/Common/Common/Authentication/IHttpRequest.cs
+++ /dev/null
@@ -1,23 +0,0 @@
-using System;
-using System.Collections.Generic;
-
-namespace GitHub.Services.Common
-{
- public interface IHttpRequest
- {
- IHttpHeaders Headers
- {
- get;
- }
-
- Uri RequestUri
- {
- get;
- }
-
- IDictionary Properties
- {
- get;
- }
- }
-}
diff --git a/src/Sdk/Common/Common/Authentication/IHttpResponse.cs b/src/Sdk/Common/Common/Authentication/IHttpResponse.cs
deleted file mode 100644
index 7bae00863..000000000
--- a/src/Sdk/Common/Common/Authentication/IHttpResponse.cs
+++ /dev/null
@@ -1,17 +0,0 @@
-using System.Net;
-
-namespace GitHub.Services.Common
-{
- public interface IHttpResponse
- {
- IHttpHeaders Headers
- {
- get;
- }
-
- HttpStatusCode StatusCode
- {
- get;
- }
- }
-}
diff --git a/src/Sdk/Common/Common/Authentication/IVssCredentialPrompt.cs b/src/Sdk/Common/Common/Authentication/IVssCredentialPrompt.cs
deleted file mode 100644
index e93992250..000000000
--- a/src/Sdk/Common/Common/Authentication/IVssCredentialPrompt.cs
+++ /dev/null
@@ -1,29 +0,0 @@
-using System.Collections.Generic;
-using System.Threading.Tasks;
-
-namespace GitHub.Services.Common
-{
- ///
- /// Provide an interface to get a new token for the credentials.
- ///
- public interface IVssCredentialPrompt
- {
- ///
- /// Get a new token using the specified provider and the previously failed token.
- ///
- /// The provider for the token to be retrieved
- /// The token which previously failed authentication, if available
- /// The new token
- Task GetTokenAsync(IssuedTokenProvider provider, IssuedToken failedToken);
-
- IDictionary Parameters { get; set; }
- }
-
- public interface IVssCredentialPrompts : IVssCredentialPrompt
- {
- IVssCredentialPrompt FederatedPrompt
- {
- get;
- }
- }
-}
diff --git a/src/Sdk/Common/Common/Authentication/IVssCredentialStorage.cs b/src/Sdk/Common/Common/Authentication/IVssCredentialStorage.cs
deleted file mode 100644
index 7a1094637..000000000
--- a/src/Sdk/Common/Common/Authentication/IVssCredentialStorage.cs
+++ /dev/null
@@ -1,23 +0,0 @@
-using System;
-
-namespace GitHub.Services.Common
-{
- public interface IVssCredentialStorage
- {
- IssuedToken RetrieveToken(
- Uri serverUrl,
- VssCredentialsType credentialsType);
-
- void StoreToken(
- Uri serverUrl,
- IssuedToken token);
-
- void RemoveToken(
- Uri serverUrl,
- IssuedToken token);
-
- bool RemoveTokenValue(
- Uri serverUrl,
- IssuedToken token);
- }
-}
diff --git a/src/Sdk/Common/Common/Authentication/IssuedToken.cs b/src/Sdk/Common/Common/Authentication/IssuedToken.cs
deleted file mode 100644
index 23aa71676..000000000
--- a/src/Sdk/Common/Common/Authentication/IssuedToken.cs
+++ /dev/null
@@ -1,113 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Threading;
-using GitHub.Services.Common.Internal;
-
-namespace GitHub.Services.Common
-{
- ///
- /// Provides a common base class for issued tokens.
- ///
- [Serializable]
- public abstract class IssuedToken
- {
- internal IssuedToken()
- {
- }
-
- ///
- /// Gets a value indicating whether or not this token has been successfully authenticated with the remote
- /// server.
- ///
- public bool IsAuthenticated
- {
- get
- {
- return m_authenticated == 1;
- }
- }
-
- protected internal abstract VssCredentialsType CredentialType
- {
- get;
- }
-
- ///
- /// True if the token is retrieved from token storage.
- ///
- internal bool FromStorage
- {
- get;
- set;
- }
-
- ///
- /// Metadata about the token in a collection of properties.
- ///
- ///
- public IDictionary Properties
- {
- get;
- set;
- }
-
- ///
- /// Id of the owner of the token.
- ///
- internal Guid UserId
- {
- get;
- set;
- }
-
- ///
- /// Name of the owner of the token.
- ///
- internal string UserName
- {
- get;
- set;
- }
-
- ///
- /// Invoked when the issued token has been validated by successfully authenticated with the remote server.
- ///
- internal bool Authenticated()
- {
- return Interlocked.CompareExchange(ref m_authenticated, 1, 0) == 0;
- }
-
- ///
- /// Get the value of the HttpHeaders.VssUserData response header and
- /// populate the UserId and UserName properties.
- ///
- internal void GetUserData(IHttpResponse response)
- {
- IEnumerable headerValues;
- if (response.Headers.TryGetValues(HttpHeaders.VssUserData, out headerValues))
- {
- string userData = headerValues.FirstOrDefault();
-
- if (!string.IsNullOrWhiteSpace(userData))
- {
- string[] split = userData.Split(':');
-
- if (split.Length >= 2)
- {
- UserId = Guid.Parse(split[0]);
- UserName = split[1];
- }
- }
- }
- }
-
- ///
- /// Applies the token to the HTTP request message.
- ///
- /// The HTTP request message
- internal abstract void ApplyTo(IHttpRequest request);
-
- private int m_authenticated;
- }
-}
diff --git a/src/Sdk/Common/Common/Authentication/IssuedTokenCredential.cs b/src/Sdk/Common/Common/Authentication/IssuedTokenCredential.cs
deleted file mode 100644
index 1bed7d720..000000000
--- a/src/Sdk/Common/Common/Authentication/IssuedTokenCredential.cs
+++ /dev/null
@@ -1,148 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Threading.Tasks;
-
-namespace GitHub.Services.Common
-{
- ///
- /// Provides a common base class for issued token credentials.
- ///
- [Serializable]
- public abstract class IssuedTokenCredential
- {
- protected IssuedTokenCredential(IssuedToken initialToken)
- {
- InitialToken = initialToken;
- }
-
- public abstract VssCredentialsType CredentialType
- {
- get;
- }
-
- ///
- /// The initial token to use to authenticate if available.
- ///
- internal IssuedToken InitialToken
- {
- get;
- set;
- }
-
- ///
- /// Gets or sets the synchronization context which should be used for UI prompts.
- ///
- internal TaskScheduler Scheduler
- {
- get
- {
- return m_scheduler;
- }
- set
- {
- m_scheduler = value;
- }
- }
-
- ///
- /// The credentials prompt which is used for retrieving a new token.
- ///
- internal IVssCredentialPrompt Prompt
- {
- get
- {
- return m_prompt;
- }
- set
- {
- m_prompt = value;
- }
- }
-
- internal IVssCredentialStorage Storage
- {
- get
- {
- return m_storage;
- }
- set
- {
- m_storage = value;
- }
- }
-
- ///
- /// The base url for the vssconnection to be used in the token storage key.
- ///
- internal Uri TokenStorageUrl { get; set; }
-
- ///
- /// Creates a token provider suitable for handling the challenge presented in the response.
- ///
- /// The targeted server
- /// The challenge response
- /// The failed token
- /// An issued token provider instance
- internal IssuedTokenProvider CreateTokenProvider(
- Uri serverUrl,
- IHttpResponse response,
- IssuedToken failedToken)
- {
- if (response != null && !IsAuthenticationChallenge(response))
- {
- throw new InvalidOperationException();
- }
-
- if (InitialToken == null && Storage != null)
- {
- if (TokenStorageUrl == null)
- {
- throw new InvalidOperationException($"The {nameof(TokenStorageUrl)} property must have a value if the {nameof(Storage)} property is set on this instance of {GetType().Name}.");
- }
- InitialToken = Storage.RetrieveToken(TokenStorageUrl, CredentialType);
- }
-
- IssuedTokenProvider provider = OnCreateTokenProvider(serverUrl, response);
- if (provider != null)
- {
- provider.TokenStorageUrl = TokenStorageUrl;
- }
-
- // If the initial token is the one which failed to authenticate, don't
- // use it again and let the token provider get a new token.
- if (provider != null)
- {
- if (InitialToken != null && !Object.ReferenceEquals(InitialToken, failedToken))
- {
- provider.CurrentToken = InitialToken;
- }
- }
-
- return provider;
- }
-
- internal virtual string GetAuthenticationChallenge(IHttpResponse webResponse)
- {
- IEnumerable values;
- if (!webResponse.Headers.TryGetValues(Internal.HttpHeaders.WwwAuthenticate, out values))
- {
- return String.Empty;
- }
-
- return String.Join(", ", values);
- }
-
- public abstract bool IsAuthenticationChallenge(IHttpResponse webResponse);
-
- protected abstract IssuedTokenProvider OnCreateTokenProvider(Uri serverUrl, IHttpResponse response);
-
- [NonSerialized]
- private TaskScheduler m_scheduler;
-
- [NonSerialized]
- private IVssCredentialPrompt m_prompt;
-
- [NonSerialized]
- private IVssCredentialStorage m_storage;
- }
-}
diff --git a/src/Sdk/Common/Common/Authentication/IssuedTokenProvider.cs b/src/Sdk/Common/Common/Authentication/IssuedTokenProvider.cs
deleted file mode 100644
index 550bbcb5c..000000000
--- a/src/Sdk/Common/Common/Authentication/IssuedTokenProvider.cs
+++ /dev/null
@@ -1,545 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.Diagnostics;
-using System.Globalization;
-using System.Threading;
-using System.Threading.Tasks;
-using GitHub.Services.Common.Diagnostics;
-
-namespace GitHub.Services.Common
-{
- internal interface ISupportSignOut
- {
- void SignOut(Uri serverUrl, Uri replyToUrl, string identityProvider);
- }
-
- ///
- /// Provides a common base class for providers of the token authentication model.
- ///
- [EditorBrowsable(EditorBrowsableState.Never)]
- public abstract class IssuedTokenProvider
- {
- private const double c_slowTokenAcquisitionTimeInSeconds = 2.0;
-
- protected IssuedTokenProvider(
- IssuedTokenCredential credential,
- Uri serverUrl,
- Uri signInUrl)
- {
- ArgumentUtility.CheckForNull(credential, "credential");
-
- this.SignInUrl = signInUrl;
- this.Credential = credential;
- this.ServerUrl = serverUrl;
-
- m_thisLock = new object();
- }
-
- ///
- /// Gets the authentication scheme used to create this token provider.
- ///
- protected virtual String AuthenticationScheme
- {
- get
- {
- return String.Empty;
- }
- }
-
- ///
- /// Gets the authentication parameter or parameters used to create this token provider.
- ///
- protected virtual String AuthenticationParameter
- {
- get
- {
- return String.Empty;
- }
- }
-
- ///
- /// Gets the credential associated with the provider.
- ///
- protected internal IssuedTokenCredential Credential
- {
- get;
- }
-
- internal VssCredentialsType CredentialType => this.Credential.CredentialType;
-
- ///
- /// Gets the current token.
- ///
- public IssuedToken CurrentToken
- {
- get;
- internal set;
- }
-
- ///
- /// Gets a value indicating whether or not a call to get token will require interactivity.
- ///
- public abstract bool GetTokenIsInteractive
- {
- get;
- }
-
- ///
- /// Gets a value indicating whether or not an ISynchronizeInvoke call is required.
- ///
- private Boolean InvokeRequired
- {
- get
- {
- return this.GetTokenIsInteractive && this.Credential.Scheduler != null;
- }
- }
-
- ///
- /// Gets the sign-in URL for the token provider.
- ///
- public Uri SignInUrl { get; private set; }
-
- protected Uri ServerUrl { get; }
-
- ///
- /// The base url for the vssconnection to be used in the token storage key.
- ///
- internal Uri TokenStorageUrl { get; set; }
-
- ///
- /// Determines whether the specified web response is an authentication challenge.
- ///
- /// The web response
- /// True if the web response is a challenge for token authentication; otherwise, false
- protected internal virtual bool IsAuthenticationChallenge(IHttpResponse webResponse)
- {
- return this.Credential.IsAuthenticationChallenge(webResponse);
- }
-
- ///
- /// Formats the authentication challenge string which this token provider handles.
- ///
- /// A string representing the handled authentication challenge
- internal string GetAuthenticationParameters()
- {
- if (string.IsNullOrEmpty(this.AuthenticationParameter))
- {
- return this.AuthenticationScheme;
- }
- else
- {
- return string.Format(CultureInfo.InvariantCulture, this.AuthenticationScheme, this.AuthenticationParameter);
- }
- }
-
- ///
- /// Validates the current token if the provided reference is the current token and it
- /// has not been validated before.
- ///
- /// The token which should be validated
- /// The web response which used the token
- internal void ValidateToken(
- IssuedToken token,
- IHttpResponse webResponse)
- {
- if (token == null)
- {
- return;
- }
-
- lock (m_thisLock)
- {
- IssuedToken tokenToValidate = OnValidatingToken(token, webResponse);
-
- if (tokenToValidate.IsAuthenticated)
- {
- return;
- }
-
- try
- {
- // Perform validation which may include matching user information from the response
- // with that from the stored connection. If user information mismatch, an exception
- // will be thrown and the token will not be authenticated, which means if the same
- // token is ever used again in a different request it will be revalidated and fail.
- tokenToValidate.GetUserData(webResponse);
- OnTokenValidated(tokenToValidate);
-
- // Set the token to be authenticated.
- tokenToValidate.Authenticated();
- }
- finally
- {
- // When the token fails validation, we null its reference from the token provider so it
- // would not be used again by the consumers of both. Note that we only update the current
- // token of the provider if it is the original token being validated, because we do not
- // want to overwrite a different token.
- if (object.ReferenceEquals(this.CurrentToken, token))
- {
- this.CurrentToken = tokenToValidate.IsAuthenticated ? tokenToValidate : null;
- }
- }
- }
- }
-
- ///
- /// Invalidates the current token if the provided reference is the current token.
- ///
- /// The token reference which should be invalidated
- internal void InvalidateToken(IssuedToken token)
- {
- bool invalidated = false;
- lock (m_thisLock)
- {
- if (token != null && object.ReferenceEquals(this.CurrentToken, token))
- {
- this.CurrentToken = null;
- invalidated = true;
- }
- }
-
- if (invalidated)
- {
- OnTokenInvalidated(token);
- }
- }
-
- ///
- /// Retrieves a token for the credentials.
- ///
- /// The token which previously failed authentication, if available
- /// The CancellationTokenthat will be assigned to the new task
- /// A security token for the current credentials
- public async Task GetTokenAsync(
- IssuedToken failedToken,
- CancellationToken cancellationToken)
- {
- IssuedToken currentToken = this.CurrentToken;
- VssTraceActivity traceActivity = VssTraceActivity.Current;
- Stopwatch aadAuthTokenTimer = Stopwatch.StartNew();
- try
- {
- VssHttpEventSource.Log.AuthenticationStart(traceActivity);
-
- if (currentToken != null)
- {
- VssHttpEventSource.Log.IssuedTokenRetrievedFromCache(traceActivity, this, currentToken);
- return currentToken;
- }
- else
- {
- GetTokenOperation operation = null;
- try
- {
- GetTokenOperation operationInProgress;
- operation = CreateOperation(traceActivity, failedToken, cancellationToken, out operationInProgress);
- if (operationInProgress == null)
- {
- return await operation.GetTokenAsync(traceActivity).ConfigureAwait(false);
- }
- else
- {
- return await operationInProgress.WaitForTokenAsync(traceActivity, cancellationToken).ConfigureAwait(false);
- }
- }
- finally
- {
- lock (m_thisLock)
- {
- m_operations.Remove(operation);
- }
-
- operation?.Dispose();
- }
- }
- }
- finally
- {
- VssHttpEventSource.Log.AuthenticationStop(traceActivity);
-
- aadAuthTokenTimer.Stop();
- TimeSpan getTokenTime = aadAuthTokenTimer.Elapsed;
-
- if(getTokenTime.TotalSeconds >= c_slowTokenAcquisitionTimeInSeconds)
- {
- // It may seem strange to pass the string value of TotalSeconds into this method, but testing
- // showed that ETW is persnickety when you register a method in an EventSource that doesn't
- // use strings or integers as its parameters. It is easier to simply give the method a string
- // than figure out to get ETW to reliably accept a double or TimeSpan.
- VssHttpEventSource.Log.AuthorizationDelayed(getTokenTime.TotalSeconds.ToString());
- }
- }
- }
-
- ///
- /// Retrieves a token for the credentials.
- ///
- /// The token which previously failed authentication, if available
- /// The CancellationTokenthat will be assigned to the new task
- /// A security token for the current credentials
- protected virtual Task OnGetTokenAsync(
- IssuedToken failedToken,
- CancellationToken cancellationToken)
- {
- if (this.Credential.Prompt != null)
- {
- return this.Credential.Prompt.GetTokenAsync(this, failedToken);
- }
- else
- {
- return Task.FromResult(null);
- }
- }
-
- ///
- /// Invoked when the current token is being validated. When overriden in a derived class,
- /// validate and return the validated token.
- ///
- /// Is called inside a lock in ValidateToken
- /// The token to validate
- /// The web response which used the token
- /// The validated token
- protected virtual IssuedToken OnValidatingToken(
- IssuedToken token,
- IHttpResponse webResponse)
- {
- return token;
- }
-
- protected virtual void OnTokenValidated(IssuedToken token)
- {
- // Store the validated token to the token storage if it is not originally from there.
- if (!token.FromStorage && TokenStorageUrl != null)
- {
- Credential.Storage?.StoreToken(TokenStorageUrl, token);
- }
-
- VssHttpEventSource.Log.IssuedTokenValidated(VssTraceActivity.Current, this, token);
- }
-
- protected virtual void OnTokenInvalidated(IssuedToken token)
- {
- if (Credential.Storage != null && TokenStorageUrl != null)
- {
- Credential.Storage.RemoveTokenValue(TokenStorageUrl, token);
- }
-
- VssHttpEventSource.Log.IssuedTokenInvalidated(VssTraceActivity.Current, this, token);
- }
-
- private GetTokenOperation CreateOperation(
- VssTraceActivity traceActivity,
- IssuedToken failedToken,
- CancellationToken cancellationToken,
- out GetTokenOperation operationInProgress)
- {
- operationInProgress = null;
- GetTokenOperation operation = null;
- lock (m_thisLock)
- {
- if (m_operations == null)
- {
- m_operations = new List();
- }
-
- // Grab the main operation which is doing the work (if any)
- if (m_operations.Count > 0)
- {
- operationInProgress = m_operations[0];
-
- // Use the existing completion source when creating the new operation
- operation = new GetTokenOperation(traceActivity, this, failedToken, cancellationToken, operationInProgress.CompletionSource);
- }
- else
- {
- operation = new GetTokenOperation(traceActivity, this, failedToken, cancellationToken);
- }
-
- m_operations.Add(operation);
- }
-
- return operation;
- }
-
- private object m_thisLock;
- private List m_operations;
-
- private class DisposableTaskCompletionSource : TaskCompletionSource, IDisposable
- {
- public DisposableTaskCompletionSource()
- {
- this.Task.ConfigureAwait(false).GetAwaiter().OnCompleted(() => { m_completed = true; });
- }
-
- ~DisposableTaskCompletionSource()
- {
- TraceErrorIfNotCompleted();
- }
-
- public void Dispose()
- {
- if (m_disposed)
- {
- return;
- }
-
- TraceErrorIfNotCompleted();
-
- m_disposed = true;
- GC.SuppressFinalize(this);
- }
-
- private void TraceErrorIfNotCompleted()
- {
- if (!m_completed)
- {
- VssHttpEventSource.Log.TokenSourceNotCompleted();
- }
- }
-
- private Boolean m_disposed;
- private Boolean m_completed;
- }
-
- private sealed class GetTokenOperation : IDisposable
- {
- public GetTokenOperation(
- VssTraceActivity activity,
- IssuedTokenProvider provider,
- IssuedToken failedToken,
- CancellationToken cancellationToken)
- : this(activity, provider, failedToken, cancellationToken, new DisposableTaskCompletionSource(), true)
- {
- }
-
- public GetTokenOperation(
- VssTraceActivity activity,
- IssuedTokenProvider provider,
- IssuedToken failedToken,
- CancellationToken cancellationToken,
- DisposableTaskCompletionSource completionSource,
- Boolean ownsCompletionSource = false)
- {
- this.Provider = provider;
- this.ActivityId = activity?.Id ?? Guid.Empty;
- this.FailedToken = failedToken;
- this.CancellationToken = cancellationToken;
- this.CompletionSource = completionSource;
- this.OwnsCompletionSource = ownsCompletionSource;
- }
-
- public Guid ActivityId { get; }
-
- public CancellationToken CancellationToken { get; }
-
- public DisposableTaskCompletionSource CompletionSource { get; }
-
- public Boolean OwnsCompletionSource { get; }
-
- private IssuedToken FailedToken { get; }
-
- private IssuedTokenProvider Provider { get; }
-
- public void Dispose()
- {
- if (this.OwnsCompletionSource)
- {
- this.CompletionSource?.Dispose();
- }
- }
-
- public async Task GetTokenAsync(VssTraceActivity traceActivity)
- {
- IssuedToken token = null;
- try
- {
- VssHttpEventSource.Log.IssuedTokenAcquiring(traceActivity, this.Provider);
- if (this.Provider.InvokeRequired)
- {
- // Post to the UI thread using the scheduler. This may return a new task object which needs
- // to be awaited, since once we get to the UI thread there may be nothing to do if someone else
- // preempts us.
-
- // The cancellation token source is used to handle race conditions between scheduling and
- // waiting for the UI task to begin execution. The callback is responsible for disposing of
- // the token source, since the thought here is that the callback will run eventually as the
- // typical reason for not starting execution within the timeout is due to a deadlock with
- // the scheduler being used.
- var timerTask = new TaskCompletionSource