protected virtual string ConnectToServer(Dev2.Data.ServiceModel.Connection connection) { // we need to grab the principle and impersonate to properly execute in context of the requesting user ;) var principle = Thread.CurrentPrincipal; var identity = principle.Identity as WindowsIdentity; WindowsImpersonationContext context = null; try { if(identity != null && connection.AuthenticationType == AuthenticationType.Windows) { context = identity.Impersonate(); } using(var client = new WebClient()) { if(connection.AuthenticationType == AuthenticationType.Windows) { client.UseDefaultCredentials = true; } else { client.UseDefaultCredentials = false; //// we to default to the hidden public user name of \, silly know but that is how to get around ntlm auth ;) if(connection.AuthenticationType == AuthenticationType.Public) { connection.UserName = GlobalConstants.PublicUsername; connection.Password = string.Empty; } client.Credentials = new NetworkCredential(connection.UserName, connection.Password); } // Need to do hub connect here to get true permissions ;) HubConnection hub = null; try { // Credentials = client.Credentials hub = new HubConnection(connection.FetchTestConnectionAddress()) { Credentials = client.Credentials }; ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate; #pragma warning disable 168 var proxy = hub.CreateHubProxy("esb"); // this is the magic line that causes proper validation #pragma warning restore 168 hub.Start().Wait(); Dev2Logger.Log.Debug("Hub State : " + hub.State); return "Success"; } catch(Exception) { // Credentials = client.Credentials var hub2 = new HubConnectionWrapperOld(connection.FetchTestConnectionAddress()) { Credentials = client.Credentials }; ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate; #pragma warning disable 168 var proxy = hub2.CreateHubProxy("esb"); // this is the magic line that causes proper validation #pragma warning restore 168 hub2.Start().Wait(); Dev2Logger.Log.Debug("Hub State : " + hub2.State); return "Success"; } finally { if(hub != null) { hub.Stop(); hub.Dispose(); } } } } finally { if(context != null && connection.AuthenticationType == AuthenticationType.Windows) { context.Undo(); } } }
ValidationResult CanConnectToServer(Dev2.Data.ServiceModel.Connection connection) { var result = new ValidationResult { ErrorFields = new ArrayList(new[] { "address" }), }; try { // Validate URI, ports, etc... // ReSharper disable ObjectCreationAsStatement new Uri(connection.Address); // ReSharper restore ObjectCreationAsStatement var connectResult = ConnectToServer(connection); if(!string.IsNullOrEmpty(connectResult)) { if(connectResult.Contains("FatalError")) { var error = XElement.Parse(connectResult); result.IsValid = false; result.ErrorMessage = string.Join(" - ", error.Nodes().Cast<XElement>().Select(n => n.Value)); } } } catch(Exception ex) { var hex = ex.InnerException as HttpClientException; if(hex != null) { result.IsValid = false; // This we know how to handle this result.ErrorMessage = "Connection Error : " + hex.Response.ReasonPhrase; return result; } result.IsValid = false; // get something more relevant ;) if(ex.Message == "One or more errors occurred." && ex.InnerException != null) { result.ErrorMessage = "Connection Error : " + ex.InnerException.Message; } else { var msg = ex.Message; if(msg.IndexOf("Connection Error : ", StringComparison.Ordinal) >= 0 || msg.IndexOf("Invalid URI:", StringComparison.Ordinal) >= 0) { result.ErrorMessage = ex.Message; } else { result.ErrorMessage = "Connection Error : " + ex.Message; } } } return result; }