UserManager.CreateUser<TUser>(IObjectSpace, IPrincipal, Action<TUser>, Boolean) 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,
IPrincipal principal,
Action<TUser> customizeUser = null,
bool autoCommit = true
)
where TUser : class, ISecurityUserWithLoginInfo
Parameters
Name | Type | Description |
---|---|---|
objectSpace | IObjectSpace | An Object Space used to create the user. |
principal | IPrincipal | A claims principal object that contains info about the user to create. |
Optional Parameters
Name | Type | Default | Description |
---|---|---|---|
customizeUser | Action<TUser> | null | A delegate method used to additionally customize the created user object. |
autoCommit | Boolean | True | A boolean value that specifies whether or not to automatically commit the changes to the Object Space. |
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
The following code snippet demonstrates a custom authentication provider implementation that uses the CreateUser
method to automatically create users for third-party OAuth2 authentication providers for testing purposes (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;
}
}
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.