CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
ApplicationContextManagerTls.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="ApplicationContextManagerTls.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Default context manager for the user property</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Security.Principal;
10using System.Threading;
11
12namespace Csla.Core
13{
21 {
22 private const string _localContextName = "Csla.LocalContext";
23 private const string _clientContextName = "Csla.ClientContext";
24
29 public bool IsStatefulContext => true;
30
34 public bool IsValid
35 {
36 get { return true; }
37 }
38
43 public virtual IPrincipal GetUser()
44 {
45 IPrincipal result = Thread.CurrentPrincipal;
46 if (result == null)
47 {
48 result = new System.Security.Claims.ClaimsPrincipal();
49 SetUser(result);
50 }
51 return result;
52 }
53
58 public virtual void SetUser(IPrincipal principal)
59 {
60 Thread.CurrentPrincipal = principal;
61 }
62
67 {
68 LocalDataStoreSlot slot = Thread.GetNamedDataSlot(_localContextName);
69 return (ContextDictionary)Thread.GetData(slot);
70 }
71
76 public void SetLocalContext(ContextDictionary localContext)
77 {
78 LocalDataStoreSlot slot = Thread.GetNamedDataSlot(_localContextName);
79 Thread.SetData(slot, localContext);
80 }
81
87 {
88 if (executionLocation == ApplicationContext.ExecutionLocations.Client)
89 {
90 return (ContextDictionary)AppDomain.CurrentDomain.GetData(_clientContextName);
91 }
92 else
93 {
94 LocalDataStoreSlot slot = Thread.GetNamedDataSlot(_clientContextName);
95 return (ContextDictionary)Thread.GetData(slot);
96 }
97 }
98
104 public void SetClientContext(ContextDictionary clientContext, ApplicationContext.ExecutionLocations executionLocation)
105 {
106 if (executionLocation == ApplicationContext.ExecutionLocations.Client)
107 {
108 AppDomain.CurrentDomain.SetData(_clientContextName, clientContext);
109 }
110 else
111 {
112 LocalDataStoreSlot slot = Thread.GetNamedDataSlot(_clientContextName);
113 Thread.SetData(slot, clientContext);
114 }
115 }
116
117 private const string _applicationContextName = "Csla.ApplicationContext";
118
123 {
124 get
125 {
126 var slot = Thread.GetNamedDataSlot(_applicationContextName);
127 return Thread.GetData(slot) as ApplicationContext;
128 }
129 set
130 {
131 var slot = Thread.GetNamedDataSlot(_applicationContextName);
132 Thread.SetData(slot, value);
133 }
134 }
135 }
136}
Provides consistent context information between the client and server DataPortal objects.
ExecutionLocations
Enum representing the locations code can execute.
Context manager for the user property and local/client/global context dictionaries that uses thread l...
void SetLocalContext(ContextDictionary localContext)
Sets the local context dictionary.
void SetClientContext(ContextDictionary clientContext, ApplicationContext.ExecutionLocations executionLocation)
Sets the client context dictionary.
ApplicationContext ApplicationContext
Gets or sets a reference to the current ApplicationContext.
virtual IPrincipal GetUser()
Gets the current user principal.
ContextDictionary GetClientContext(ApplicationContext.ExecutionLocations executionLocation)
Gets the client context dictionary.
virtual void SetUser(IPrincipal principal)
Sets the current user principal.
bool IsValid
Returns a value indicating whether the context is valid.
ContextDictionary GetLocalContext()
Gets the local context dictionary.
bool IsStatefulContext
Gets a value indicating whether the context manager is stateful.
Dictionary type that is serializable with the SerializationFormatterFactory.GetFormatter().
Defines the interface for an application context manager type.