UserManager.AddLogin<TUser>(TUser, String, String) Method
Adds login information required to store the specified user’s login data for the specified authentication provider.
Namespace: DevExpress.ExpressApp.Security
Assembly: DevExpress.ExpressApp.Security.v25.2.dll
NuGet Package: DevExpress.ExpressApp.Security
Declaration
public void AddLogin<TUser>(
TUser user,
string loginProviderName,
string providerUserKey
)
where TUser : class, ISecurityUserWithLoginInfo
Parameters
| Name | Type | Description |
|---|---|---|
| user | TUser | An application user object. |
| loginProviderName | String | The name of the login provider for which to add login information. |
| providerUserKey | String | The user login for the specified login provider. |
Type Parameters
| Name | Description |
|---|---|
| TUser | The user object type. |
Remarks
Review the following topic for information on the mechanism that XAF uses to associate multiple authentication methods with a single user:
- ISecurityUserWithLoginInfo
- Implement a Security System User (ApplicationUser) Based on XAF Business Classes
- Implement a Custom Security System User Based on an Existing Business Class - Support the ISecurityUserWithLoginInfo Interface
The following code snippet demonstrates how to use the AddLogin method to assign login information for the WindowsAuthentication authentication scheme to an administrative account. The Template Kit generates equivalent Module Updater code to automatically register an administrative account for debugging purposes.
File: MySolution.Module\DatabaseUpdate\Updater.cs
using DevExpress.ExpressApp.Updating;
using DevExpress.ExpressApp.Security;
// ...
public class Updater : ModuleUpdater {
// ...
public override void UpdateDatabaseAfterUpdateSchema() {
// ...
#if !RELEASE
UserManager userManager = ObjectSpace.ServiceProvider.GetRequiredService<UserManager>();
string autogeneratedAdminUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
if(userManager.FindUserByName<ApplicationUser>(ObjectSpace, autogeneratedAdminUserName) == null) {
string EmptyPassword = "";
_ = userManager.CreateUser<ApplicationUser>(ObjectSpace, autogeneratedAdminUserName, EmptyPassword, (user) => {
user.Roles.Add(adminRole);
userManager.AddLogin(user, SecurityDefaults.WindowsAuthentication, autogeneratedAdminUserName);
});
}
ObjectSpace.CommitChanges();
}
#endif
private PermissionPolicyRole CreateDefaultRole() {
// ...
}
}