Class UserService
Service for managing users.
Implements
Inherited Members
Namespace: DataWeb.Identity
Assembly: DataWeb.Core.dll
Syntax
public class UserService : IUserService
Remarks
Initializes a new instance of the UserService class.
Constructors
UserService(IHttpService, IOptions<IdentityOptions>, IHtmlService, IRazorTemplateService, IItemService, IEmailService, IConfigurationService, IUserStore, IRoleStore, IUserLoginStore, IUserSettingStore, IStringLocalizer)
Service for managing users.
Declaration
public UserService(IHttpService httpService, IOptions<IdentityOptions> identityOptions, IHtmlService htmlService, IRazorTemplateService templateService, IItemService itemService, IEmailService emailService, IConfigurationService configurationService, IUserStore userStore, IRoleStore roleStore, IUserLoginStore userLoginStore, IUserSettingStore userSettingStore, IStringLocalizer localizer)
Parameters
| Type | Name | Description |
|---|---|---|
| IHttpService | httpService | The HTTP service. |
| IOptions<IdentityOptions> | identityOptions | |
| IHtmlService | htmlService | The HTML service. |
| IRazorTemplateService | templateService | The template service. |
| IItemService | itemService | The item service. |
| IEmailService | emailService | The email service. |
| IConfigurationService | configurationService | The configuration service. |
| IUserStore | userStore | The user store. |
| IRoleStore | roleStore | The role store. |
| IUserLoginStore | userLoginStore | The user login store. |
| IUserSettingStore | userSettingStore | The user setting store. |
| IStringLocalizer | localizer | The localizer. |
Remarks
Initializes a new instance of the UserService class.
Properties
AppSettings
Gets the application settings.
Declaration
public ConfigurationAppSettings AppSettings { get; }
Property Value
| Type | Description |
|---|---|
| ConfigurationAppSettings |
Methods
AddUserLoginAsync(string, string, string, CancellationToken)
Adds a new external login for the user with the specified userIdMaster, using the given providerKey and loginProvider.
Declaration
public Task AddUserLoginAsync(string userIdMaster, string providerKey, string loginProvider, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | userIdMaster | The Id of the user to add the external login for. |
| string | providerKey | The provider key of the external login to add. |
| string | loginProvider | The login provider of the external login to add. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when |
CreateUserAsync(AppIdentityUser, CancellationToken)
Creates a new user with the provided user object.
Declaration
public Task<UserService.EditResult> CreateUserAsync(AppIdentityUser user, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| AppIdentityUser | user | The AppIdentityUser object representing the user to create. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<UserService.EditResult> | Returns an UserService.EditResult object representing the result of the operation. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the |
DeleteUserAsync(AppIdentityUser, CancellationToken)
Deletes the user with the specified Id.
Declaration
public Task DeleteUserAsync(AppIdentityUser user, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| AppIdentityUser | user | The AppIdentityUser object representing the user to delete. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task | Returns a Task representing the completion of the operation. |
EmailExistsAsync(string, CancellationToken)
Determines whether a user with the given email exists in the system.
Declaration
public Task<bool> EmailExistsAsync(string email, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | The email of the user to check. |
|
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<bool> | A boolean indicating whether the user exists. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the email parameter is null or empty. |
GetAllUserSettingsAsync(string, string, CancellationToken)
Gets all user settings for a given user Id and navigation path.
Declaration
public Task<IEnumerable<UserSetting>> GetAllUserSettingsAsync(string userIdMaster, string navigationPath = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | userIdMaster | The Id of the user to get settings for. |
| string | navigationPath | The navigation path to filter the settings by. Can be null to get all settings. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<IEnumerable<UserSetting>> | An enumerable collection of user settings. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when |
GetRolesAsync(CancellationToken)
Gets all the roles.
Declaration
public Task<IEnumerable<AppIdentityRole>> GetRolesAsync(CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<IEnumerable<AppIdentityRole>> | An enumerable of AppIdentityRole objects. |
GetUserByEmailAsync(string, PublishMode?, CancellationToken)
Retrieves an AppIdentityUser by their email address.
Declaration
public Task<AppIdentityUser> GetUserByEmailAsync(string email, PublishMode? publishMode = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | The email address of the user to retrieve. |
|
| PublishMode? | publishMode | Optional parameter to specify the publish mode of the user. If not specified, all users will be searched regardless of PublishMode. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<AppIdentityUser> | Returns an AppIdentityUser object if found, otherwise returns null. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the provided |
See Also
GetUserByIdMasterAsync(string, PublishMode?, CancellationToken)
Retrieves an AppIdentityUser by their userIdMaster.
Declaration
public Task<AppIdentityUser> GetUserByIdMasterAsync(string userIdMaster, PublishMode? publishMode = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | userIdMaster | The Id of the user to retrieve. |
| PublishMode? | publishMode | Optional parameter to specify the publish mode of the user. If not specified, all users will be searched regardless of PublishMode. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<AppIdentityUser> | Returns an AppIdentityUser object if found, otherwise returns null. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the provided |
See Also
GetUserByUserNameAsync(string, PublishMode?, CancellationToken)
Retrieves an AppIdentityUser by their username.
Declaration
public Task<AppIdentityUser> GetUserByUserNameAsync(string userName, PublishMode? publishMode = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | userName | The username of the user to retrieve. |
| PublishMode? | publishMode | Optional parameter to specify the publish mode of the user. If not specified, all users will be searched regardless of PublishMode. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<AppIdentityUser> | Returns an AppIdentityUser object if found, otherwise returns null. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the provided |
See Also
GetUserLoginAsync(string, string, CancellationToken)
Retrieves a user login by provider key and login provider.
Declaration
public Task<UserLogin> GetUserLoginAsync(string providerKey, string loginProvider, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | providerKey | The provider key of the user login. |
| string | loginProvider | The login provider of the user login. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<UserLogin> | The user login with the specified provider key and login provider. |
GetUserLoginsAsync(string, CancellationToken)
Retrieves a collection of UserLogin objects for the user with the specified user Id.
Declaration
public Task<IEnumerable<UserLogin>> GetUserLoginsAsync(string userIdMaster, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | userIdMaster | The Id of the user to retrieve the UserLogins for. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<IEnumerable<UserLogin>> | A collection of UserLogin objects for the user with the specified user Id. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when UserIdMaster is not defined. |
GetUserRolesAsync(string, CancellationToken)
Get the roles for a user by user name.
Declaration
public Task<string[]> GetUserRolesAsync(string userName, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | userName | The user name to retrieve roles for. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<string[]> | An array of strings representing the names of the roles the user belongs to. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the user name is null or empty. |
GetUserSettingAsync(string, string, string, CancellationToken)
Gets a user setting value by its name and optional navigation path.
Declaration
public Task<string> GetUserSettingAsync(string userIdMaster, string name, string navigationPath = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | userIdMaster | The user IdMaster. |
| string | name | The name of the setting. |
| string | navigationPath | Optional navigation path for nested settings. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<string> | The value of the setting, or null if the setting is not found. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when |
GetUserValueByEmailAsync(string, string, CancellationToken)
Retrieves a specific user value by email address from the user store.
Declaration
public Task<object> GetUserValueByEmailAsync(string email, string name, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | The email address of the user to retrieve the value for. |
|
| string | name | The name of the user value to retrieve. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<object> | An object representing the value for the specified user value name. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the email is null or empty. |
GetUserValueByIdMasterAsync(string, string, CancellationToken)
Retrieves a specific value for a user based on the user's Id.
Declaration
public Task<object> GetUserValueByIdMasterAsync(string userIdMaster, string name, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | userIdMaster | The Id of the user to retrieve the value for. |
| string | name | The name of the value to retrieve. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<object> | An object representing the value retrieved from the user's data. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the provided |
GetUserValueByUserNameAsync(string, string, CancellationToken)
Retrieves the value of a specific property for a user with the given username.
Declaration
public Task<object> GetUserValueByUserNameAsync(string userName, string name, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | userName | The username of the user to retrieve the property for. |
| string | name | The name of the property to retrieve the value for. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<object> | The value of the property for the user with the given username. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the username is null or empty. |
GetValidationError(string, string)
Gets a validation error object based on the provided error code and message.
Declaration
public ValidationError GetValidationError(string code, string message)
Parameters
| Type | Name | Description |
|---|---|---|
| string | code | The error code. |
| string | message | The error message. |
Returns
| Type | Description |
|---|---|
| ValidationError | A ValidationError object. |
InitIdentityUser(string, string)
Initializes a new AppIdentityUser object with the provided userName and email.
Declaration
public AppIdentityUser InitIdentityUser(string userName, string email)
Parameters
| Type | Name | Description |
|---|---|---|
| string | userName | The username to use for the user, or null/empty if the IsUserName setting is false. |
| string | The email address to use for the user. |
Returns
| Type | Description |
|---|---|
| AppIdentityUser | Returns a new AppIdentityUser object initialized with the provided values. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the IsUserName setting is true and |
IsEmailAvailableAsync(string, string, CancellationToken)
Checks if the specified email is available for registration. Throws an exception if email is null or empty.
Declaration
public Task<bool> IsEmailAvailableAsync(string email, string userIdMaster, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | The email to check availability for. |
|
| string | userIdMaster | The IdMaster of the user to check for, to exclude it from the search. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<bool> | True if the email is available, false otherwise. |
IsUserInRoleAsync(string, string, CancellationToken)
Checks if a user with the given user name is in a given role.
Declaration
public Task<bool> IsUserInRoleAsync(string userName, string roleName, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | userName | The user name to check. |
| string | roleName | The role to check. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<bool> | True if the user is in the role, false otherwise. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when either the user name or the role name is null or empty. |
IsUserNameAvailableAsync(string, string, CancellationToken)
Check if a given username is available to use for a new user or for updating an existing user, identified by the given userIdMaster.
Declaration
public Task<bool> IsUserNameAvailableAsync(string userName, string userIdMaster, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | userName | The username to check availability for. |
| string | userIdMaster | The Id of the user to check availability for (for updating an existing user), or null for new user registration. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<bool> | True if the username is available, false if it is already taken. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown if the given username is null or empty. |
RemoveAllUserSettingsAsync(string, CancellationToken)
Removes all settings for a given user.
Declaration
public Task RemoveAllUserSettingsAsync(string userIdMaster, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | userIdMaster | The Id of the user to remove settings for. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task | A task representing the asynchronous operation. |
RemoveUserLoginAsync(string, string, string, CancellationToken)
Removes a login for a user.
Declaration
public Task RemoveUserLoginAsync(string userIdMaster, string providerKey, string loginProvider, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | userIdMaster | The user Id. |
| string | providerKey | The provider key for the login. |
| string | loginProvider | The login provider name. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task | A Task representing the asynchronous operation. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when |
RemoveUserSettingAsync(string, string, string, CancellationToken)
Removes a user setting for the specified user and name.
Declaration
public Task RemoveUserSettingAsync(string userIdMaster, string name, string navigationPath = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | userIdMaster | The unique identifier of the user. |
| string | name | The name of the user setting to remove. |
| string | navigationPath | The navigation path of the user setting to remove. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task | A task that represents the asynchronous operation. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when |
SendConfirmEmailAsync(AppIdentityUser, string, CancellationToken)
Sends an email to the specified identityUser to confirm their email address.
Declaration
public Task SendConfirmEmailAsync(AppIdentityUser identityUser, string emailConfirmUrl, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| AppIdentityUser | identityUser | The AppIdentityUser object representing the user to send the confirmation email to. |
| string | emailConfirmUrl | The URL to include in the confirmation email for the user to click on. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task | Returns a Task representing the completion of the operation. |
SendPasswordRemindEmailAsync(AppIdentityUser, string, CancellationToken)
Sends an email to the specified identityUser with a link to reset their password.
Declaration
public Task SendPasswordRemindEmailAsync(AppIdentityUser identityUser, string resetPasswordUrl, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| AppIdentityUser | identityUser | The AppIdentityUser object representing the user to send the password reminder email to. |
| string | resetPasswordUrl | The URL to include in the email for the user to click on to reset their password. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task | Returns a Task representing the completion of the operation. |
SendPasswordResetEmailAsync(AppIdentityUser, CancellationToken)
Sends an email to the specified identityUser to reset their password.
Declaration
public Task SendPasswordResetEmailAsync(AppIdentityUser identityUser, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| AppIdentityUser | identityUser | The AppIdentityUser object representing the user to send the password reset email to. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task | Returns a Task representing the completion of the operation. |
SendTwoFactorEmailAsync(AppIdentityUser, string, CancellationToken)
Sends an email to the specified identityUser with the two factor authentication code.
Declaration
public Task SendTwoFactorEmailAsync(AppIdentityUser identityUser, string code, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| AppIdentityUser | identityUser | The AppIdentityUser object representing the user to send the confirmation email to. |
| string | code | The two factor code. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task | Returns a Task representing the completion of the operation. |
SetIdentityClaims(ClaimsIdentity, AppIdentityUser)
Sets the ClaimsIdentity for the given user.
Declaration
public void SetIdentityClaims(ClaimsIdentity identity, AppIdentityUser user)
Parameters
| Type | Name | Description |
|---|---|---|
| ClaimsIdentity | identity | The ClaimsIdentity to update. |
| AppIdentityUser | user | The AppIdentityUser to extract claims from. |
SetStatusMessage(string)
Sets the message to be displayed to the user after a certain action is performed.
Declaration
public StatusMessage SetStatusMessage(string messageType)
Parameters
| Type | Name | Description |
|---|---|---|
| string | messageType | The type of the message to be displayed. |
Returns
| Type | Description |
|---|---|
| StatusMessage | Returns a StatusMessage object representing the message to be displayed. |
SetUserSettingAsync(string, string, string, string, CancellationToken)
Sets the value of a user setting for the specified user with the given name and value.
Declaration
public Task SetUserSettingAsync(string userIdMaster, string name, string value, string navigationPath = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | userIdMaster | The Id of the user whose setting should be set. |
| string | name | The name of the setting to set. |
| string | value | The value to set the setting to. |
| string | navigationPath | The optional navigation path to limit the scope of the setting to. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task | A Task representing the asynchronous operation. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown if userIdMaster or name is null or empty. |
UpdateUserAsync(AppIdentityUser, CancellationToken)
Updates the user data for the provided user object.
Declaration
public Task<UserService.EditResult> UpdateUserAsync(AppIdentityUser user, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| AppIdentityUser | user | The AppIdentityUser object representing the user data to update. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<UserService.EditResult> | Returns an UserService.EditResult object representing the result of the operation. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the Id value of the provided |
UserHasPasswordAsync(string, CancellationToken)
Determines whether the user with the specified userIdMaster has a password set.
Declaration
public Task<bool> UserHasPasswordAsync(string userIdMaster, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | userIdMaster | The Id of the user to check. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<bool> | Returns a Task object representing the asynchronous operation, and containing a bool value indicating whether the user has a password set. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown if the |