UserManagerWebApiService
using Polly;
using Relativity.Transfer.Resources;
using System;
using System.Globalization;
using System.Net;
namespace Relativity.Transfer.WebApi
{
[Obsolete("WebAPI support will be removed in upcoming releases. In order to communicate with Relativity use Kepler Services. For more information, see KeplerServiceBase class.")]
internal sealed class UserManagerWebApiService : IUserManagerService, IDisposable
{
private readonly ClientConfiguration configuration;
private readonly ITransferLog log;
private bool initialized;
private bool disposed;
public RelativityConnectionInfo ConnectionInfo { get; }
public string DistributedToken { get; set; }
public int MaxRetryAttempts { get; set; }
public double TimeoutSeconds { get; set; }
public UserManagerWebApiService(RelativityConnectionInfo connectionInfo, ClientConfiguration configuration, ITransferLog log)
{
if (connectionInfo == null)
throw new ArgumentNullException("connectionInfo");
if (configuration == null)
throw new ArgumentNullException("configuration");
if (log == null)
throw new ArgumentNullException("log");
if (!configuration.UseLegacyWebApi)
throw new ArgumentException(CoreStrings.InvalidCommunicationModeExceptionMessage, "UseLegacyWebApi");
this.configuration = configuration;
ConnectionInfo = connectionInfo;
this.log = log;
DistributedToken = null;
MaxRetryAttempts = configuration.MaxHttpRetryAttempts;
TimeoutSeconds = configuration.HttpTimeoutSeconds;
}
public void Login()
{
Initialize();
Policy.Handle<Exception>().WaitAndRetry(MaxRetryAttempts, (int retryAttempt) => TimeSpan.FromSeconds(Math.Pow(2, (double)retryAttempt)), delegate(Exception exception, TimeSpan span) {
log.LogError(exception, $"""{span}", Array.Empty<object>());
}).Execute(delegate {
UserManager userManager = new UserManager();
try {
userManager.set_Credentials(WebApiService.GetWebApiHttpCredential(ConnectionInfo).CreateCredentials());
userManager.set_CookieContainer(configuration.CookieContainer);
userManager.set_Timeout((int)TimeSpan.FromSeconds(TimeoutSeconds).TotalMilliseconds);
userManager.set_Url(UrlHelper.Combine(WebApiService.GetWebApiServiceUrl(ConnectionInfo, log), "UserManager.asmx").ToString());
userManager.ClearCookiesBeforeLogin();
NetworkCredential networkCredential = ConnectionInfo.Credential.CreateCredentials() as NetworkCredential;
if (networkCredential == null)
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, CoreStrings.HttpCredentialNotSupportedMessage, ConnectionInfo.Credential.GetType().ToString()));
if (!GlobalSettings.Instance.CommandLineModeEnabled)
log.LogInformation("Preparing to perform a login via distributed API...", Array.Empty<object>());
if (!(ConnectionInfo.Credential is IntegratedSecurityCredential) && !userManager.Login(networkCredential.UserName, networkCredential.Password))
throw new InvalidOperationException(CoreStrings.DistributedLoginFailedMessage);
if (!GlobalSettings.Instance.CommandLineModeEnabled) {
log.LogInformation("Successfully performed a login using the distributed API.", Array.Empty<object>());
log.LogInformation("Preparing to request a distributed login token...", Array.Empty<object>());
}
DistributedToken = userManager.GenerateDistributedAuthenticationToken();
if (string.IsNullOrWhiteSpace(DistributedToken))
throw new InvalidOperationException(CoreStrings.DistributedTokenInvalidMessage);
if (!GlobalSettings.Instance.CommandLineModeEnabled)
log.LogInformation("Successfully retrieved a distributed login token.", Array.Empty<object>());
} finally {
((IDisposable)userManager)?.Dispose();
}
});
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!disposed)
disposed = true;
}
private void Initialize()
{
if (!initialized) {
ServicePointManager.SecurityProtocol = (SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12);
initialized = true;
}
}
}
}