From bce693a82ba6b94d460675c926f156e776888d16 Mon Sep 17 00:00:00 2001 From: Rockford Lhotka Date: Tue, 14 Sep 2021 12:00:07 -0500 Subject: [PATCH 1/2] Clean up obsolete code --- Source/Csla.Shared/DataPortalT.cs | 41 ------------------------------- 1 file changed, 41 deletions(-) diff --git a/Source/Csla.Shared/DataPortalT.cs b/Source/Csla.Shared/DataPortalT.cs index 29f9e0f228..2bfac29fe8 100644 --- a/Source/Csla.Shared/DataPortalT.cs +++ b/Source/Csla.Shared/DataPortalT.cs @@ -29,47 +29,6 @@ public class DataPortal : IDataPortal /// public Csla.Core.ContextDictionary GlobalContext { get; set; } - private class DataPortalAsyncRequest - { - public object Argument { get; set; } - public System.Security.Principal.IPrincipal Principal { get; set; } - public Csla.Core.ContextDictionary ClientContext { get; set; } - public Csla.Core.ContextDictionary GlobalContext { get; set; } - public object UserState { get; set; } - // passes CurrentCulture and CurrentUICulture to the async thread - public CultureInfo CurrentCulture; - public CultureInfo CurrentUICulture; - - public DataPortalAsyncRequest(object argument, object userState) - { - this.Argument = argument; - this.Principal = Csla.ApplicationContext.User; - this.ClientContext = Csla.ApplicationContext.ClientContext; -#pragma warning disable CS0618 // Type or member is obsolete - this.GlobalContext = Csla.ApplicationContext.GlobalContext; -#pragma warning restore CS0618 // Type or member is obsolete - this.UserState = userState; - this.CurrentCulture = System.Globalization.CultureInfo.CurrentCulture; - this.CurrentUICulture = System.Globalization.CultureInfo.CurrentUICulture; - } - } - - private class DataPortalAsyncResult - { - public T Result { get; set; } - public Csla.Core.ContextDictionary GlobalContext { get; set; } - public object UserState { get; set; } - public Exception Error { get; set; } - - public DataPortalAsyncResult(T result, Csla.Core.ContextDictionary globalContext, Exception error, object userState) - { - this.Result = result; - this.GlobalContext = globalContext; - this.UserState = userState; - this.Error = error; - } - } - private async Task DoCreateAsync(Type objectType, object criteria, bool isSync) { Server.DataPortalResult result = null; From 79baf8b219c933eed5618e8e5144901e21510aa9 Mon Sep 17 00:00:00 2001 From: Rockford Lhotka Date: Tue, 14 Sep 2021 12:00:58 -0500 Subject: [PATCH 2/2] #2485 Flow all context values to logical server in all cases --- Source/Csla.Shared/Server/DataPortal.cs | 74 ++++++------------- .../Csla.Shared/Server/DataPortalContext.cs | 29 ++------ 2 files changed, 32 insertions(+), 71 deletions(-) diff --git a/Source/Csla.Shared/Server/DataPortal.cs b/Source/Csla.Shared/Server/DataPortal.cs index 9f0abae4c9..f8e73002da 100644 --- a/Source/Csla.Shared/Server/DataPortal.cs +++ b/Source/Csla.Shared/Server/DataPortal.cs @@ -674,79 +674,53 @@ internal void Initialize(InterceptArgs e) private void SetContext(DataPortalContext context) { + // set the logical execution location _oldLocation = Csla.ApplicationContext.LogicalExecutionLocation; ApplicationContext.SetLogicalExecutionLocation(ApplicationContext.LogicalExecutionLocations.Server); - if (!context.IsRemotePortal && ApplicationContext.WebContextManager != null && !ApplicationContext.WebContextManager.IsValid) + if (context.IsRemotePortal) { - // local data portal and no valid HttpContext - // if context already exists, then use existing context (from AsyncLocal or TLS) - if (ApplicationContext.ClientContext == null || ApplicationContext.ClientContext.Count == 0) - ApplicationContext.SetContext(context.ClientContext, context.GlobalContext); + // indicate that the code is physically running on the server + ApplicationContext.SetExecutionLocation(ApplicationContext.ExecutionLocations.Server); } - // if the dataportal is not remote then - // do nothing - if (!context.IsRemotePortal) return; - - // set the context value so everyone knows the - // code is running on the server - ApplicationContext.SetExecutionLocation(ApplicationContext.ExecutionLocations.Server); - - // set the app context to the value we got from the - // client + // set the app context to the value we got from the caller ApplicationContext.SetContext(context.ClientContext, context.GlobalContext); - // set the thread's culture to match the client -#if !PCL46 && !PCL259// rely on NuGet bait-and-switch for actual implementation -#if NETCORE - System.Globalization.CultureInfo.CurrentCulture = - new System.Globalization.CultureInfo(context.ClientCulture); - System.Globalization.CultureInfo.CurrentUICulture = - new System.Globalization.CultureInfo(context.ClientUICulture); -#elif NETFX_CORE - var list = new System.Collections.ObjectModel.ReadOnlyCollection(new List { context.ClientUICulture }); - Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Languages = list; - list = new System.Collections.ObjectModel.ReadOnlyCollection(new List { context.ClientCulture }); - Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Languages = list; -#else - System.Threading.Thread.CurrentThread.CurrentCulture = - new System.Globalization.CultureInfo(context.ClientCulture); - System.Threading.Thread.CurrentThread.CurrentUICulture = - new System.Globalization.CultureInfo(context.ClientUICulture); -#endif -#endif + // set the thread's culture to match the caller + SetCulture(context); + + // set current user principal + SetPrincipal(context); + } + private static void SetPrincipal(DataPortalContext context) + { if (ApplicationContext.AuthenticationType == "Windows") { // When using integrated security, Principal must be null if (context.Principal != null) - { - Csla.Security.SecurityException ex = - new Csla.Security.SecurityException(Resources.NoPrincipalAllowedException); - //ex.Action = System.Security.Permissions.SecurityAction.Deny; - throw ex; - } -#if !(ANDROID || IOS) && !NETFX_CORE + throw new Csla.Security.SecurityException(Resources.NoPrincipalAllowedException); // Set .NET to use integrated security AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); -#endif } else { - // We expect the some Principal object + // We expect some Principal object if (context.Principal == null) - { - Csla.Security.SecurityException ex = - new Csla.Security.SecurityException( - Resources.BusinessPrincipalException + " Nothing"); - //ex.Action = System.Security.Permissions.SecurityAction.Deny; - throw ex; - } + throw new Csla.Security.SecurityException(Resources.BusinessPrincipalException + " Nothing"); ApplicationContext.User = context.Principal; } } + private static void SetCulture(DataPortalContext context) + { + System.Threading.Thread.CurrentThread.CurrentCulture = + new System.Globalization.CultureInfo(context.ClientCulture); + System.Threading.Thread.CurrentThread.CurrentUICulture = + new System.Globalization.CultureInfo(context.ClientUICulture); + } + private void ClearContext(DataPortalContext context) { ApplicationContext.SetLogicalExecutionLocation(_oldLocation); diff --git a/Source/Csla.Shared/Server/DataPortalContext.cs b/Source/Csla.Shared/Server/DataPortalContext.cs index 8a1b840c57..6a551af1f5 100644 --- a/Source/Csla.Shared/Server/DataPortalContext.cs +++ b/Source/Csla.Shared/Server/DataPortalContext.cs @@ -106,27 +106,14 @@ public ObjectFactoryAttribute FactoryInfo /// Indicates whether the DataPortal is remote. public DataPortalContext(IPrincipal principal, bool isRemotePortal) { - if (isRemotePortal) - { - _principal = principal; - _remotePortal = isRemotePortal; -#if NETFX_CORE - _clientCulture = System.Globalization.CultureInfo.CurrentCulture.Name; - _clientUICulture = System.Globalization.CultureInfo.CurrentUICulture.Name; -#else - _clientCulture = - System.Threading.Thread.CurrentThread.CurrentCulture.Name; - _clientUICulture = - System.Threading.Thread.CurrentThread.CurrentUICulture.Name; -#endif - _clientContext = Csla.ApplicationContext.ContextManager.GetClientContext(); - _globalContext = Csla.ApplicationContext.ContextManager.GetGlobalContext(); - } - else if (ApplicationContext.WebContextManager != null && ApplicationContext.WebContextManager.IsValid) - { - _clientContext = Csla.ApplicationContext.ContextManager.GetClientContext(); - _globalContext = Csla.ApplicationContext.ContextManager.GetGlobalContext(); - } + _principal = principal; + _remotePortal = isRemotePortal; + _clientCulture = + System.Threading.Thread.CurrentThread.CurrentCulture.Name; + _clientUICulture = + System.Threading.Thread.CurrentThread.CurrentUICulture.Name; + _clientContext = Csla.ApplicationContext.ContextManager.GetClientContext(); + _globalContext = Csla.ApplicationContext.ContextManager.GetGlobalContext(); } ///