From 63d3fc31f9031e530b65b048ca5cb13f9c05286b Mon Sep 17 00:00:00 2001 From: Christophe Gondouin <14264672+cgoconseils@users.noreply.github.com> Date: Wed, 20 Dec 2023 23:50:25 +0100 Subject: [PATCH] Correction on HybridConnection Uri (allows to activate Require Client Authorization) --- .../RemoteDebugger/Plugin.partial.cs | 120 +++++++++--------- 1 file changed, 59 insertions(+), 61 deletions(-) diff --git a/src/XrmFramework.Plugin/RemoteDebugger/Plugin.partial.cs b/src/XrmFramework.Plugin/RemoteDebugger/Plugin.partial.cs index 3aebb708..cd86afa7 100644 --- a/src/XrmFramework.Plugin/RemoteDebugger/Plugin.partial.cs +++ b/src/XrmFramework.Plugin/RemoteDebugger/Plugin.partial.cs @@ -19,90 +19,88 @@ private bool SendToRemoteDebugger(LocalPluginContext localContext) localContext.Log("The context is genuine"); localContext.Log($"UnSecuredConfig : {UnSecuredConfigFull}"); - //if (!string.IsNullOrEmpty(UnSecuredConfig) && UnSecuredConfig.Contains("debugSessions")) - //{ - // var debuggerUnsecuredConfig = JsonConvert.DeserializeObject(UnSecuredConfig); + var initiatingUserId = localContext.GetInitiatingUserId(); + var rootUserId = localContext.GetRootUserId(); - // localContext.Log($"Debug session ids : {string.Join(",", debuggerUnsecuredConfig.DebugSessionIds)}"); + localContext.Log($"Initiating user Id : {initiatingUserId}, root user Id : {rootUserId}"); - var initiatingUserId = localContext.GetInitiatingUserId(); - var rootUserId = localContext.GetRootUserId(); + var queryDebugSessions = BindingModelHelper.GetRetrieveAllQuery(); + queryDebugSessions.Criteria.AddCondition(DebugSessionDefinition.Columns.DebugeeId, ConditionOperator.In, + initiatingUserId, rootUserId); + queryDebugSessions.Criteria.AddCondition(DebugSessionDefinition.Columns.StateCode, + ConditionOperator.Equal, DebugSessionState.Active.ToInt()); - localContext.Log($"Initiating user Id : {initiatingUserId}, root user Id : {rootUserId}"); + var debugSession = localContext.AdminOrganizationService.RetrieveAll(queryDebugSessions) + .FirstOrDefault(); - var queryDebugSessions = BindingModelHelper.GetRetrieveAllQuery(); - queryDebugSessions.Criteria.AddCondition(DebugSessionDefinition.Columns.DebugeeId, ConditionOperator.In, initiatingUserId, rootUserId); - queryDebugSessions.Criteria.AddCondition(DebugSessionDefinition.Columns.StateCode, ConditionOperator.Equal, DebugSessionState.Active.ToInt()); + localContext.Log($"Debug session : {debugSession}"); - //queryDebugSessions.Criteria.AddCondition(DebugSessionDefinition.Columns.Id, ConditionOperator.In, debuggerUnsecuredConfig.DebugSessionIds.Cast().ToArray()); + if (debugSession == null || debugSession.SessionEnd < DateTime.Today) + { + return false; + } - var debugSession = localContext.AdminOrganizationService.RetrieveAll(queryDebugSessions).FirstOrDefault(); + var remoteContext = localContext.RemoteContext; + remoteContext.Id = Guid.NewGuid(); + remoteContext.TypeAssemblyQualifiedName = GetType().AssemblyQualifiedName; + remoteContext.UnsecureConfig = UnSecuredConfigFull; + remoteContext.SecureConfig = SecuredConfig; - localContext.Log($"Debug session : {debugSession}"); + var uri = new Uri($"{debugSession.RelayUrl.TrimEnd('/')}/{debugSession.HybridConnectionName}"); - if (debugSession != null) - { - if (debugSession.SessionEnd >= DateTime.Today) - { - - var remoteContext = localContext.RemoteContext; - remoteContext.Id = Guid.NewGuid(); - remoteContext.TypeAssemblyQualifiedName = GetType().AssemblyQualifiedName; - remoteContext.UnsecureConfig = UnSecuredConfigFull; - remoteContext.SecureConfig = SecuredConfig; - - var uri = new Uri($"{debugSession.RelayUrl}/{debugSession.HybridConnectionName}"); + try + { + using var hybridConnection = new HybridConnection(debugSession.SasKeyName, + debugSession.SasConnectionKey, uri.AbsoluteUri); + var message = new RemoteDebuggerMessage(RemoteDebuggerMessageType.Context, remoteContext, + remoteContext.Id); - try - { - using var hybridConnection = new HybridConnection(debugSession.SasKeyName, debugSession.SasConnectionKey, uri.AbsoluteUri); - var message = new RemoteDebuggerMessage(RemoteDebuggerMessageType.Context, remoteContext, remoteContext.Id); - - RemoteDebuggerMessage response; - while (true) - { - localContext.Log("Sending context to local machine : {0}", message); + RemoteDebuggerMessage response; + while (true) + { + localContext.Log("Sending context to local machine : {0}", message); - response = hybridConnection.SendMessage(message).GetAwaiter().GetResult(); + response = hybridConnection.SendMessage(message).GetAwaiter().GetResult(); - localContext.Log("Received response : {0}", response); + localContext.Log("Received response : {0}", response); - if (response.MessageType == RemoteDebuggerMessageType.Context || response.MessageType == RemoteDebuggerMessageType.Exception) - { - break; - } + if (response.MessageType == RemoteDebuggerMessageType.Context || + response.MessageType == RemoteDebuggerMessageType.Exception) + { + break; + } - var request = response.GetOrganizationRequest(); + var request = response.GetOrganizationRequest(); - var service = response.UserId.HasValue ? localContext.GetService(response.UserId.Value) : localContext.AdminOrganizationService; + var service = response.UserId.HasValue + ? localContext.GetService(response.UserId.Value) + : localContext.AdminOrganizationService; - var organizationResponse = service.Execute(request); + var organizationResponse = service.Execute(request); - message = new RemoteDebuggerMessage(RemoteDebuggerMessageType.Response, organizationResponse, remoteContext.Id); - } + message = new RemoteDebuggerMessage(RemoteDebuggerMessageType.Response, + organizationResponse, remoteContext.Id); + } - if (response.MessageType == RemoteDebuggerMessageType.Exception) - { - throw response.GetException(); - } + if (response.MessageType == RemoteDebuggerMessageType.Exception) + { + throw response.GetException(); + } - var updatedContext = response.GetContext(); + var updatedContext = response.GetContext(); - localContext.UpdateContext(updatedContext); + localContext.UpdateContext(updatedContext); - return true; - } - catch (HttpRequestException) - { - // Run the plugin as deploy if the remote debugger is not connected - } - } - } - //} + return true; + } + catch (HttpRequestException) + { + // Run the plugin as deploy if the remote debugger is not connected + } } return false; } } -} +} \ No newline at end of file