Back to Devexpress

UserManager.AddLogin<TUser>(TUser, String, String) Method

expressappframework-devexpress-dot-expressapp-dot-security-dot-usermanager-dot-addlogin-1-x28-0-system-dot-string-system-dot-string-x29.md

latest4.3 KB
Original Source

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

csharp
public void AddLogin<TUser>(
    TUser user,
    string loginProviderName,
    string providerUserKey
)
    where TUser : class, ISecurityUserWithLoginInfo
vb
Public Sub AddLogin(Of TUser As {Class, ISecurityUserWithLoginInfo})(
    user As TUser,
    loginProviderName As String,
    providerUserKey As String
)

Parameters

NameTypeDescription
userTUser

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

NameDescription
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:

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

csharp
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

UserManager Class

UserManager Members

DevExpress.ExpressApp.Security Namespace