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.
Csla.Blazor.WebAssembly/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//-----------------------------------------------------------------------
8using Csla.Core;
9using Microsoft.AspNetCore.Components.Authorization;
10using System;
11using System.Security.Claims;
12using System.Security.Principal;
13using System.Threading.Tasks;
14
16{
21 public class ApplicationContextManager : IContextManager, IDisposable
22 {
23 private ContextDictionary LocalContext { get; set; }
24 private ContextDictionary ClientContext { get; set; }
25 private IPrincipal CurrentPrincipal { get; set; }
26 private readonly ClaimsPrincipal UnauthenticatedPrincipal = new();
27 private bool disposedValue;
28
33
38
44 public ApplicationContextManager(AuthenticationStateProvider authenticationStateProvider)
45 {
46 AuthenticationStateProvider = authenticationStateProvider;
47 CurrentPrincipal = UnauthenticatedPrincipal;
48 AuthenticationStateProvider.AuthenticationStateChanged += AuthenticationStateProvider_AuthenticationStateChanged;
49 InitializeUser();
50 }
51
52 private void InitializeUser()
53 {
54 AuthenticationStateProvider_AuthenticationStateChanged(AuthenticationStateProvider.GetAuthenticationStateAsync());
55 }
56
57 private void AuthenticationStateProvider_AuthenticationStateChanged(Task<AuthenticationState> task)
58 {
59 task.ContinueWith((t) =>
60 {
61 if (task.IsCompletedSuccessfully && task.Result != null)
62 CurrentPrincipal = task.Result.User;
63 else
64 CurrentPrincipal = UnauthenticatedPrincipal;
65 });
66 }
67
73 public bool IsValid
74 {
75 get { return true; }
76 }
77
82 public bool IsStatefulContext => true;
83
87 public IPrincipal GetUser()
88 {
89 return CurrentPrincipal;
90 }
91
98 public virtual void SetUser(IPrincipal principal)
99 {
100 CurrentPrincipal = principal;
101 }
102
107 {
108 if (LocalContext == null)
109 LocalContext = new ContextDictionary();
110 return LocalContext;
111 }
112
117 public void SetLocalContext(ContextDictionary localContext)
118 {
119 LocalContext = localContext;
120 }
121
127 {
128 if (ClientContext == null)
129 ClientContext = new ContextDictionary();
130 return ClientContext;
131 }
132
138 public void SetClientContext(ContextDictionary clientContext, ApplicationContext.ExecutionLocations executionLocation)
139 {
140 ClientContext = clientContext;
141 }
142
147 protected virtual void Dispose(bool disposing)
148 {
149 if (!disposedValue)
150 {
151 if (disposing)
152 {
153 AuthenticationStateProvider.AuthenticationStateChanged -= AuthenticationStateProvider_AuthenticationStateChanged;
154 }
155 disposedValue = true;
156 }
157 }
158
162 public void Dispose()
163 {
164 // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
165 Dispose(disposing: true);
166 GC.SuppressFinalize(this);
167 }
168 }
169}
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...
AuthenticationStateProvider AuthenticationStateProvider
Gets the current HttpContext instance.
void SetClientContext(ContextDictionary clientContext, ApplicationContext.ExecutionLocations executionLocation)
Sets the client context.
bool IsValid
Gets a value indicating whether this context manager is valid for use in the current environment.
bool IsStatefulContext
Gets a value indicating whether the current runtime is stateful (e.g.
ContextDictionary GetClientContext(ApplicationContext.ExecutionLocations executionLocation)
Gets the client context.
virtual void SetUser(IPrincipal principal)
Sets the current principal ONLY IN APPLICATIONCONTEXT.
virtual void Dispose(bool disposing)
Dispose this object's resources.
ApplicationContextManager(AuthenticationStateProvider authenticationStateProvider)
Creates an instance of the object, initializing it with the required IServiceProvider.
void SetLocalContext(ContextDictionary localContext)
Sets the local context.
Dictionary type that is serializable with the SerializationFormatterFactory.GetFormatter().
Defines the interface for an application context manager type.