Back to Devexpress

UserManager Class

expressappframework-devexpress-dot-expressapp-dot-security-b7a83e71.md

latest4.2 KB
Original Source

UserManager Class

Exposes API required to manage user objects in the database. Use Dependency Injection to access this class’s instance in an application.

Namespace : DevExpress.ExpressApp.Security

Assembly : DevExpress.ExpressApp.Security.v25.2.dll

NuGet Package : DevExpress.ExpressApp.Security

Declaration

csharp
public sealed class UserManager
vb
Public NotInheritable Class UserManager

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 Template Kit generates equivalent logic in the application’s Module Updater code.

File: MySolution.Module/DatabaseUpdate/Updater.cs

csharp
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.

Inheritance

Object UserManager

See Also

UserManager Members

User Logon and Authentication

Authenticate and Authorize Web API Endpoints

DevExpress.ExpressApp.Security Namespace