expressappframework-devexpress-dot-expressapp-dot-security-dot-usermanager-dot-addlogin-1-x28-0-system-dot-string-system-dot-string-x29.md
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
public void AddLogin<TUser>(
TUser user,
string loginProviderName,
string providerUserKey
)
where TUser : class, ISecurityUserWithLoginInfo
Public Sub AddLogin(Of TUser As {Class, ISecurityUserWithLoginInfo})(
user As TUser,
loginProviderName As String,
providerUserKey As String
)
| 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.
|
| Name | Description |
|---|---|
| TUser |
The user object type.
|
Review the following topic for information on the mechanism that XAF uses to associate multiple authentication methods with a single user:
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() {
// ...
}
}
See Also