UserManager Class
Exposes API required to manage user objects in the database. Use Dependency Injection to access this class’s instance in a .NET 6+ application.
Namespace: DevExpress.ExpressApp.Security
Assembly: DevExpress.ExpressApp.Security.v24.1.dll
NuGet Package: DevExpress.ExpressApp.Security
#Declaration
#Remarks
The UserManager
service exposes methods that you can use to create and access application users and their login information. You can also use UserManager
to interact with the user lockout mechanism.
The following code snippet demonstrates how to use the UserManager
service to create a new application user. The XAF Solution Wizard generates equivalent logic in the application’s Module Updater code.
File: MySolution.Module/DatabaseUpdate/Updater.cs
using DevExpress.ExpressApp.Security;
// ...
public class Updater : ModuleUpdater {
// ...
public override void UpdateDatabaseAfterUpdateSchema() {
base.UpdateDatabaseAfterUpdateSchema();
UserManager userManager = ObjectSpace.ServiceProvider.GetRequiredService<UserManager>();
// If a user named 'User' doesn't exist in the database, create this user.
if(userManager.FindUserByName<ApplicationUser>(ObjectSpace, "User") == null) {
// Set a password if the standard authentication type is used.
string EmptyPassword = "";
var userCreationResult = userManager.CreateUser<ApplicationUser>(ObjectSpace, "User", EmptyPassword, (user) => {
// Add the Users role to the user
user.Roles.Add(defaultRole);
});
// ...
}
// ...
ObjectSpace.CommitChanges(); // This line persists created object(s).
}
}
For more use case scenarios, refer to the descriptions of the UserManager
class members. For example:
- AddLogin - Adds login method information to a user record.
- FindUserByName - Finds an application user object based on a user name.
- GetAuthenticationToken - Generates an authentication token for a user with the capability to add additional claims to the token.
- GetCurrentPrincipal - Gets the current user’s
ClaimsPrincipal
object. - IsLockedOut - Demonstrates how to interact with the user lockout mechanism.