UserManager.CreateUser<TUser>(IObjectSpace, String, String, Action<TUser>) Method
Creates a new user with the specified settings.
Namespace: DevExpress.ExpressApp.Security
Assembly: DevExpress.ExpressApp.Security.v24.2.dll
NuGet Package: DevExpress.ExpressApp.Security
Declaration
public UserResult<TUser> CreateUser<TUser>(
IObjectSpace objectSpace,
string userName,
string password,
Action<TUser> customizeUser = null
)
where TUser : class, ISecurityUserWithLoginInfo, IAuthenticationStandardUser
Parameters
Name | Type | Description |
---|---|---|
objectSpace | IObjectSpace | An Object Space used to create the user. |
userName | String | A user name for the new user. |
password | String | A password for the new user. |
Optional Parameters
Name | Type | Default | Description |
---|---|---|---|
customizeUser | Action<TUser> | null | A delegate method used to additionally customize the created user object. |
Type Parameters
Name | Description |
---|---|
TUser | A user object type. |
Returns
Type | Description |
---|---|
DevExpress.ExpressApp.Security.UserResult<TUser> | An object that contains the result of the user creation operation. |
Remarks
Use this method to create and customize a new application user as the following code snippet demonstrates:
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).
}
}
The CreateUser
method returns a UserResult
object. This object exposes the following properties:
Succeeded
- A
boolean
property that indicates whether or not a user was created successfully. User
- The created user object.
Error
- If user creation fails, this property contains the resulting Exception.