Skip to main content
All docs
V24.1
.NET 6.0+
  • The page you are viewing does not exist in the .NET Framework 4.5.2+ platform documentation. This link will take you to the parent topic of the current section.

UserManager.FindUserByPrincipal<TUser>(IObjectSpace, IPrincipal) Method

Finds an application user based on the specified claims principal.

Namespace: DevExpress.ExpressApp.Security

Assembly: DevExpress.ExpressApp.Security.v24.1.dll

NuGet Package: DevExpress.ExpressApp.Security

Declaration

public TUser FindUserByPrincipal<TUser>(
    IObjectSpace objectSpace,
    IPrincipal principal
)
    where TUser : class, ISecurityUserWithLoginInfo

Parameters

Name Type Description
objectSpace IObjectSpace

An Object Space used to search for a user.

principal IPrincipal

A claims principal object.

Type Parameters

Name Description
TUser

The user object type.

Returns

Type Description
TUser

The resulting user object.

Remarks

The following code snippet demonstrates a custom authentication provider implementation that uses the FindUserByPrincipal method to search for a user object based on a ClaimsPrincipal object returned by a third-party authentication service (XAF Solution Wizard generates equivalent code for applications configured to use OAuth2).

public class CustomAuthenticationProvider : IAuthenticationProviderV2 {
    private readonly UserManager userManager;

    public CustomAuthenticationProvider(UserManager userManager) {
        this.userManager = userManager;
    }

    public object Authenticate(IObjectSpace objectSpace) {
        var currentPrincipal = userManager.GetCurrentPrincipal();
        if(currentPrincipal?.Identity?.IsAuthenticated ?? false) {
            var user = userManager.FindUserByPrincipal<ApplicationUser>(objectSpace, currentPrincipal);
            if(user != null) {
                return new UserResult<ApplicationUser>(user);
            }

        // The code below creates users for testing purposes only.
#if !RELEASE
            bool autoCreateUser = true;
            if(autoCreateUser) {
                var userResult = userManager.CreateUser<ApplicationUser>(objectSpace, currentPrincipal, user => {
                    user.Roles.Add(objectSpace.FirstOrDefault<PermissionPolicyRole>(role => role.Name == "Default"));
                });
                if(!userResult.Succeeded) {
                    //throw userResult.Error;
                }
                return userResult;
            }
#endif
        }
        return null;
    }
}
See Also