Back to Devexpress

UserManager.FindUserByName<TUser>(IObjectSpace, String) Method

expressappframework-devexpress-dot-expressapp-dot-security-dot-usermanager-dot-finduserbyname-1-x28-devexpress-dot-expressapp-dot-iobjectspace-system-dot-string-x29.md

latest6.2 KB
Original Source

UserManager.FindUserByName<TUser>(IObjectSpace, String) Method

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

Declaration

csharp
public TUser FindUserByName<TUser>(
    IObjectSpace objectSpace,
    string name
)
    where TUser : class, ISecurityUser
vb
Public Function FindUserByName(Of TUser As {Class, ISecurityUser})(
    objectSpace As IObjectSpace,
    name As String
) As TUser

Parameters

NameTypeDescription
objectSpaceIObjectSpace

An Object Space used to search for a user.

| | name | String |

A System.String value that contains the user name.

|

Type Parameters

NameDescription
TUser

The user object type.

|

Returns

TypeDescription
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

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).
    }
}

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.

XAF_logon-form-manage-users-register-a-new-user-restore-a-password/CS/EFCore/DXApplication1.Module/DatabaseUpdate/Updater.cs#L28

csharp
var userManager = objectSpace.ServiceProvider.GetRequiredService<UserManager>();
if (userManager.FindUserByName<ApplicationUser>(objectSpace, userName) != null) {
    throw new UserFriendlyException("A user already exists with this name.");

connect-winforms-grid-to-backend-using-middletier-server/CS/DataModel.Shared/DatabaseUpdate/Updater.cs#L32

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

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

connect-winforms-grid-to-backend-using-middletier-server/VB/DataModel.Shared/DatabaseUpdate/Updater.vb#L40

vb
' 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

FindUserByKey

FindUserByLogin

FindUserByPrincipal

UserManager Class

UserManager Members

DevExpress.ExpressApp.Security Namespace