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.
ApplicationContextManagerHttpContext.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="ApplicationContextManager.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Application context manager that uses HttpContextAccessor</summary>
7//-----------------------------------------------------------------------
8using Csla.Core;
9using Csla.Runtime;
10using Microsoft.AspNetCore.Http;
11using System;
12using System.Security.Claims;
13
15{
21 {
22
23 private const string _localContextName = "Csla.LocalContext";
24 private const string _clientContextName = "Csla.ClientContext";
25
26 private readonly IRuntimeInfo runtimeInfo;
27
28
29#if NET5_0_OR_GREATER
33 protected Blazor.ActiveCircuitState ActiveCircuitState { get; private set; }
34
42 public ApplicationContextManagerHttpContext(IHttpContextAccessor httpContextAccessor, IRuntimeInfo runtimeInfo, Blazor.ActiveCircuitState activeCircuitState)
43 {
44 HttpContext = httpContextAccessor.HttpContext;
45 this.runtimeInfo = runtimeInfo;
46 ActiveCircuitState = activeCircuitState;
47 }
48#else
55 public ApplicationContextManagerHttpContext(IHttpContextAccessor httpContextAccessor, IRuntimeInfo runtimeInfo)
56 {
57 HttpContext = httpContextAccessor.HttpContext;
58 this.runtimeInfo = runtimeInfo;
59 }
60
61#endif
62
63
67 protected virtual HttpContext HttpContext { get; private set; }
68
74 public bool IsValid
75 {
76 get
77 {
78 if (HttpContext is null)
79 return false;
80
81 if (runtimeInfo.LocalProxyNewScopeExists)
82 return false;
83
84#if NET5_0_OR_GREATER
85 if (ActiveCircuitState.CircuitExists)
86 return false;
87#endif
88
89 return true;
90 }
91 }
92
97 public bool IsStatefulContext => false;
98
102 public System.Security.Principal.IPrincipal GetUser()
103 {
104 var result = HttpContext?.User;
105 if (result == null)
106 {
107 result = new Csla.Security.CslaClaimsPrincipal();
108 SetUser(result);
109 }
110 return result;
111 }
112
117 public void SetUser(System.Security.Principal.IPrincipal principal)
118 {
119 HttpContext.User = (ClaimsPrincipal)principal;
120 }
121
126 {
127 return (ContextDictionary)HttpContext?.Items[_localContextName];
128 }
129
134 public void SetLocalContext(ContextDictionary localContext)
135 {
136 HttpContext.Items[_localContextName] = localContext;
137 }
138
144 {
145 return (ContextDictionary)HttpContext?.Items[_clientContextName];
146 }
147
153 public void SetClientContext(ContextDictionary clientContext, ApplicationContext.ExecutionLocations executionLocation)
154 {
155 HttpContext.Items[_clientContextName] = clientContext;
156 }
157
158 private const string _applicationContextName = "Csla.ApplicationContext";
159
164 {
165 get
166 {
167 return (ApplicationContext)HttpContext?.Items[_applicationContextName];
168 }
169 set
170 {
171 HttpContext.Items[_applicationContextName] = value;
172 }
173 }
174 }
175}
Provides consistent context information between the client and server DataPortal objects.
ExecutionLocations
Enum representing the locations code can execute.
Application context manager that uses HttpContextAccessor when resolving HttpContext to store context...
void SetClientContext(ContextDictionary clientContext, ApplicationContext.ExecutionLocations executionLocation)
Sets the client context.
System.Security.Principal.IPrincipal GetUser()
Gets the current principal.
ApplicationContextManagerHttpContext(IHttpContextAccessor httpContextAccessor, IRuntimeInfo runtimeInfo)
Creates an instance of the object, initializing it with the required IServiceProvider.
virtual HttpContext HttpContext
Gets the current HttpContext instance.
void SetUser(System.Security.Principal.IPrincipal principal)
Sets the current principal.
bool IsStatefulContext
Gets a value indicating whether the context manager is stateful.
void SetLocalContext(ContextDictionary localContext)
Sets the local context.
bool IsValid
Gets a value indicating whether this context manager is valid for use in the current environment.
ContextDictionary GetClientContext(ApplicationContext.ExecutionLocations executionLocation)
Gets the client context.
Dictionary type that is serializable with the SerializationFormatterFactory.GetFormatter().
ClaimsPrincipal subclass that supports serialization by SerializationFormatterFactory....
Defines the interface for an application context manager type.
Information about the current runtime environment.
Definition: IRuntimeInfo.cs:14