我必须从Excel工作表中的CMS.User表(FirstName,LastName,UserName,Email,UserPassword和UserPasswordFormat)中上载约500个用户。

如何加密密码并将其保存在数据库中?

var newUser = new UserInfo
{
    UserName = userDto.Email,
    UserEnabled = true,
    FirstName = userDto.FirstName,
    LastName = userDto.LastName,
    FullName = userDto.FirstName + " " + userDto.LastName,
    Email = userDto.Email,
    UserPasswordFormat = userDto.UserPasswordFormat,
    SiteIndependentPrivilegeLevel = CMS.Base.UserPrivilegeLevelEnum.None
};

var newUserSettings = newUser.UserSettings ?? new UserSettingsInfo();

newUser.SetValue("UserPassword", userDto.UserPassword);

newUserSettings.UserPhone = userDto.PhoneNumber;

UserInfoProvider.SetUserInfo(newUser);

UserInfoProvider.AddUserToSite(newUser.UserName, site.SiteName);

最佳答案

用你的代码代替

newUser.SetValue("UserPassword", userDto.UserPassword);


您应该使用:

UserInfoProvider.SetUserInfo(newUser); // saves user to DB
UserInfoProvider.SetPassword(newUser, userDto.UserPassword); // saves hashed password


Kentico还将CMSHashStringSalt应用程序设置用于哈希密码。

10-06 12:19