UserManager.FindUserByName<TUser>(IObjectSpace, String) Method
Finds an application user based on the specified user name.
Namespace: DevExpress.ExpressApp.Security
Assembly: DevExpress.ExpressApp.Security.v24.2.dll
NuGet Package: DevExpress.ExpressApp.Security
Declaration
public TUser FindUserByName<TUser>(
IObjectSpace objectSpace,
string name
)
where TUser : class, ISecurityUser
Parameters
Name | Type | Description |
---|---|---|
objectSpace | IObjectSpace | An Object Space used to search for a user. |
name | String | A |
Type Parameters
Name | Description |
---|---|
TUser | The user object type. |
Returns
Type | Description |
---|---|
TUser | The resulting user object. |
Remarks
Use this method to search for a user object based on a user name 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).
}
}
See Also