expressappframework-devexpress-dot-expressapp-dot-security-dot-usermanager-dot-finduserbyname-1-x28-devexpress-dot-expressapp-dot-iobjectspace-system-dot-string-x29.md
Finds an application user based on the specified user name.
Namespace : DevExpress.ExpressApp.Security
Assembly : DevExpress.ExpressApp.Security.v25.2.dll
NuGet Package : DevExpress.ExpressApp.Security
public TUser FindUserByName<TUser>(
IObjectSpace objectSpace,
string name
)
where TUser : class, ISecurityUser
Public Function FindUserByName(Of TUser As {Class, ISecurityUser})(
objectSpace As IObjectSpace,
name As String
) As TUser
| Name | Type | Description |
|---|---|---|
| objectSpace | IObjectSpace |
An Object Space used to search for a user.
| | name | String |
A System.String value that contains the user name.
|
| Name | Description |
|---|---|
| TUser |
The user object type.
|
| Type | Description |
|---|---|
| TUser |
The resulting user object.
|
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).
}
}
The following code snippets (auto-collected from DevExpress Examples) contain references to the FindUserByName<TUser>(IObjectSpace, String) method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
var userManager = objectSpace.ServiceProvider.GetRequiredService<UserManager>();
if (userManager.FindUserByName<ApplicationUser>(objectSpace, userName) != null) {
throw new UserFriendlyException("A user already exists with this name.");
// If a user named 'User' doesn't exist in the database, create this user
if(userManager.FindUserByName<ApplicationUser>(ObjectSpace, DefaultUserName) == null) {
// Set a password if the standard authentication type is used
xaf-custom-logon-parameters/CS/EF/EFCoreCustomLogonAll.Module/DatabaseUpdate/Updater.cs#L34
UserManager userManager = ObjectSpace.ServiceProvider.GetRequiredService<UserManager>();
ApplicationUser userAdmin = userManager.FindUserByName<ApplicationUser>(ObjectSpace, "Admin");
// If a user named 'Admin' doesn't exist in the database, create this user.
' If a user named 'User' doesn't exist in the database, create this user
If userManager.FindUserByName(Of ApplicationUser)(ObjectSpace, DefaultUserName) Is Nothing Then
' Set a password if the standard authentication type is used
See Also