CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
Csla.AspNetCore.Shared/ApplicationContextManager.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//-----------------------------------------------------------------------
8#if !BLAZOR
9using Csla.Core;
10using Microsoft.AspNetCore.Http;
11using Microsoft.Extensions.DependencyInjection;
12using System;
13using System.Security.Claims;
14
16{
22 {
23
24 private const string _localContextName = "Csla.LocalContext";
25 private const string _clientContextName = "Csla.ClientContext";
26 private const string _globalContextName = "Csla.GlobalContext";
27
28 private IServiceProvider _serviceProvider;
29
35 public ApplicationContextManager(IServiceProvider serviceProvider)
36 {
37 _serviceProvider = serviceProvider;
38 }
39
43 protected virtual HttpContext HttpContext
44 {
45 get
46 {
47 HttpContext result = null;
48 if (_serviceProvider != null)
49 {
50 var httpContextAccessor = (IHttpContextAccessor)_serviceProvider.GetService(typeof(IHttpContextAccessor));
51 if (httpContextAccessor != null)
52 {
53 result = httpContextAccessor.HttpContext;
54 }
55 }
56 return result;
57 }
58 }
59
65 public bool IsValid
66 {
67 get { return HttpContext != null; }
68 }
69
73 public System.Security.Principal.IPrincipal GetUser()
74 {
75 var result = HttpContext?.User;
76 if (result == null)
77 {
78 result = new Csla.Security.CslaClaimsPrincipal();
79 SetUser(result);
80 }
81 return result;
82 }
83
88 public void SetUser(System.Security.Principal.IPrincipal principal)
89 {
90 HttpContext.User = (ClaimsPrincipal)principal;
91 }
92
97 {
98 return (ContextDictionary)HttpContext?.Items[_localContextName];
99 }
100
105 public void SetLocalContext(ContextDictionary localContext)
106 {
107 HttpContext.Items[_localContextName] = localContext;
108 }
109
114 {
115 return (ContextDictionary)HttpContext?.Items[_clientContextName];
116 }
117
122 public void SetClientContext(ContextDictionary clientContext)
123 {
124 HttpContext.Items[_clientContextName] = clientContext;
125 }
126
131 {
132 return (ContextDictionary)HttpContext?.Items[_globalContextName];
133 }
134
139 public void SetGlobalContext(ContextDictionary globalContext)
140 {
141 HttpContext.Items[_globalContextName] = globalContext;
142 }
143
147 public IServiceProvider GetDefaultServiceProvider()
148 {
149 // on aspnet core we proactively detect request scope, before falling back to root application scope.
150 // this saves users from having to SetServiceProvider() at the start of every request.
151 return HttpContext?.RequestServices ?? _serviceProvider;
152 }
153
158 public void SetDefaultServiceProvider(IServiceProvider serviceProvider)
159 {
160 // Service provider to be used as fallback when there is no more specific scoped service provider available.
161 _serviceProvider = serviceProvider;
162 }
163
168 public IServiceProvider GetServiceProvider()
169 {
170 return (IServiceProvider)ApplicationContext.LocalContext["__sps"] ?? GetDefaultServiceProvider();
171 }
172
177 public void SetServiceProvider(IServiceProvider scope)
178 {
179 Csla.ApplicationContext.LocalContext["__sps"] = scope;
180 }
181 }
182}
183#endif
Application context manager that uses HttpContextAccessor when resolving HttpContext to store context...
void SetServiceProvider(IServiceProvider scope)
Sets the service provider for current scope
void SetDefaultServiceProvider(IServiceProvider serviceProvider)
Sets the default IServiceProvider
ApplicationContextManager(IServiceProvider serviceProvider)
Creates an instance of the object, initializing it with the required IServiceProvider.
void SetGlobalContext(ContextDictionary globalContext)
Sets the global context.
void SetLocalContext(ContextDictionary localContext)
Sets the local context.
void SetUser(System.Security.Principal.IPrincipal principal)
Sets the current principal.
System.Security.Principal.IPrincipal GetUser()
Gets the current principal.
virtual HttpContext HttpContext
Gets the current HttpContext instance.
void SetClientContext(ContextDictionary clientContext)
Sets the client context.
bool IsValid
Gets a value indicating whether this context manager is valid for use in the current environment.
IServiceProvider GetDefaultServiceProvider()
Gets the default IServiceProvider
IServiceProvider GetServiceProvider()
Gets the service provider for current scope
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.