Class SecurityService
Provides a high-level IPrincipal API over the generalised implementations of authentication and authorisation.
public sealed class SecurityService
- Inheritance
-
SecurityService
- Inherited Members
Constructors
SecurityService(IAuthentication, IAuthorisation, bool, Func<string, IPrincipal, IScopedContext>, Func<PermissionSetBuilder>, ILogger, RoleCache?, Func<Task>?)
public SecurityService(IAuthentication authentication, IAuthorisation authorisation, bool requireTenancySpecification, Func<string, IPrincipal, IScopedContext> createScopedContext, Func<PermissionSetBuilder> createBuilder, ILogger logger, RoleCache? roleCache, Func<Task>? resetCache = null)
Parameters
authenticationIAuthenticationauthorisationIAuthorisationrequireTenancySpecificationboolcreateScopedContextFunc<string, IPrincipal, IScopedContext>createBuilderFunc<PermissionSetBuilder>loggerILoggerroleCacheRoleCacheresetCacheFunc<Task>
Properties
CurrentPrincipal
The current user session, set by LoginAsync(Credentials?, CancellationToken?) and LogoutAsync(CancellationToken?), which will be used to create contexts.
public IPrincipal CurrentPrincipal { get; }
Property Value
Methods
CreateAuthenticatedPrincipalAsync(Identity, IEntityContext, Credentials?, CancellationToken?)
Create a security context for an identity that has already been authenticated by some means the framework doesn't implement, subject to standard authorisation.
public Task<IPrincipal> CreateAuthenticatedPrincipalAsync(Identity identity, IEntityContext scopedContext, Credentials? credentials = null, CancellationToken? cancellationToken = null)
Parameters
identityIdentityThe identity to authorise. It is taken as given: IAuthentication is not consulted.
scopedContextIEntityContextA context with unrestricted read access, used to look the identity's roles up.
credentialsCredentialsOptional credentials to carry on the principal, for onward use by RPC.
cancellationTokenCancellationToken?
Returns
Remarks
For sign-in mechanisms where possession of a secret takes the place of credentials, such as a single-use link. Unlike CreateTrustedPrincipal(string, params string[]), the resulting principal holds only the permissions IAuthorisation grants the identity's roles. Does not set CurrentPrincipal.
CreateLocalPrincipal(Credentials?)
Perform authentication, if doing so doesn't require async I/O, and create a security context.
public IPrincipal CreateLocalPrincipal(Credentials? credentials = null)
Parameters
credentialsCredentialsOptional login credentials (required by some authenticators).
Returns
Remarks
If async is required, an unauthenticated principal will be returned - use CreateStandardPrincipalAsync(Credentials?, CancellationToken?) in that case.
CreateLocalPrincipal(Identity, Credentials?)
Perform authentication, if doing so doesn't require async I/O, and create a security context.
public IPrincipal CreateLocalPrincipal(Identity identity, Credentials? credentials = null)
Parameters
identityIdentityA claimed identity to verify.
credentialsCredentialsOptional login credentials (required by some authenticators).
Returns
Remarks
If async is required, an unauthenticated principal will be returned - use CreateStandardPrincipalAsync(Identity, Credentials?, CancellationToken?) in that case.
CreateStandardPrincipalAsync(Credentials?, CancellationToken?)
Perform authentication and create a security context.
public Task<IPrincipal> CreateStandardPrincipalAsync(Credentials? credentials = null, CancellationToken? cancellationToken = null)
Parameters
credentialsCredentialsOptional login credentials (required by some authenticators).
cancellationTokenCancellationToken?
Returns
Remarks
Does not set CurrentPrincipal; this method is for uncached authentication.
CreateStandardPrincipalAsync(Identity, Credentials?, CancellationToken?)
Perform authentication and create a security context.
public Task<IPrincipal> CreateStandardPrincipalAsync(Identity identity, Credentials? credentials = null, CancellationToken? cancellationToken = null)
Parameters
identityIdentityA claimed identity to verify.
credentialsCredentialsOptional login credentials (required by some authenticators).
cancellationTokenCancellationToken?
Returns
Remarks
Does not set CurrentPrincipal; this method is for uncached authentication.
CreateTrustedPrincipal(string, Action<PermissionSetBuilder>, params string[])
Create a security context which is always considered to be authenticated, using the specified authorisation.
public IPrincipal CreateTrustedPrincipal(string username, Action<PermissionSetBuilder> buildPermissions, params string[] roles)
Parameters
usernamestringbuildPermissionsAction<PermissionSetBuilder>rolesstring[]
Returns
CreateTrustedPrincipal(string, params string[])
Create a security context which is always considered to be authenticated and authorised.
public IPrincipal CreateTrustedPrincipal(string username, params string[] roles)
Parameters
Returns
CreateTrustedPrincipal(string, PermissionSet, params string[])
Create a security context which is always considered to be authenticated, using the specified authorisation.
public IPrincipal CreateTrustedPrincipal(string username, PermissionSet permissions, params string[] roles)
Parameters
usernamestringpermissionsPermissionSetrolesstring[]
Returns
CreateUntrustedPrincipal(AuthenticationFailure?)
Create a security context which is never considered to be authenticated or authorised.
public IPrincipal CreateUntrustedPrincipal(AuthenticationFailure? error = null)
Parameters
errorAuthenticationFailureOptional reason the principal is unauthenticated (default: NotLoggedIn).
Returns
GetRolePermissions(Role)
public PermissionSet GetRolePermissions(Role role)
Parameters
roleRole
Returns
InvalidateAuthorisation(Identity?)
Invalidate cached roles for an identity, or for all identities if none is specified.
public void InvalidateAuthorisation(Identity? identity = null)
Parameters
identityIdentity
Remarks
Useful if you know that permissions have changed and need them to be reloaded at next login.
LoginAsync(Credentials?, CancellationToken?)
Attempt to acquire an authenticated principal, replacing the CurrentPrincipal with the result.
public Task<IPrincipal> LoginAsync(Credentials? credentials = null, CancellationToken? cancellationToken = null)
Parameters
credentialsCredentialsOptional login credentials (required by some authenticators).
cancellationTokenCancellationToken?
Returns
LoginAsync(Identity, Credentials?, CancellationToken?)
Attempt to acquire an authenticated principal, replacing the CurrentPrincipal with the result.
public Task<IPrincipal> LoginAsync(Identity identity, Credentials? credentials = null, CancellationToken? cancellationToken = null)
Parameters
identityIdentityA claimed identity to verify.
credentialsCredentialsOptional login credentials (required by some authenticators).
cancellationTokenCancellationToken?
Returns
LogoutAsync(CancellationToken?)
Deauthenticate the the CurrentPrincipal.
public Task LogoutAsync(CancellationToken? cancellationToken = null)
Parameters
cancellationTokenCancellationToken?
Returns
LogoutAsync(AuthenticationFailure, CancellationToken?)
Deauthenticate the the CurrentPrincipal.
public Task LogoutAsync(AuthenticationFailure error, CancellationToken? cancellationToken = null)
Parameters
errorAuthenticationFailureReason for deauthentication.
cancellationTokenCancellationToken?
Returns
QuiesceAsync(CancellationToken?)
Wait for any in-progress principal change to settle: its PrincipalChanged handlers and any session cache reset.
public Task QuiesceAsync(CancellationToken? cancellationToken = null)
Parameters
cancellationTokenCancellationToken?
Returns
Remarks
Used by tests to ensure a stable state before assertions.
SpecifyTenancy(Tenancy)
Request some level of tenant access for the current principal. RequireTenancySpecification must be set.
public void SpecifyTenancy(Tenancy requestedTenancy)
Parameters
requestedTenancyTenancy
Remarks
This method cannot increase the access level beyond what the principal is authorised for; passing UniversalTenancy as an argument just means that you'll get all the permissions returned by IAuthorisation for your roles.
Events
PrincipalChanged
Raised when CurrentPrincipal changes.
public event PrincipalChangedEventHandler? PrincipalChanged