以编程方式创建时未保存 Sitecore 用户配置文件设置
Sitecore User Profile Settings not saved when created programmatically
我正在 Sitecore 站点 (Sitecore 7.0) 上构建注册组件,在创建用户的同时,none 正在创建自定义配置文件或角色。日志中没有错误。创建的用户具有正确的用户名和域,但没有自定义配置文件并且缺少全名、电子邮件地址、角色和其他自定义字段。有没有人在实现这个方面遇到过相关的困难?
N.b。使用电子邮件地址作为用户名,如果这会有什么不同的话。我已经更新了配置以允许这样做。我还在下面添加了一些日志记录,它们都输出了正确的信息。
这是代码示例:
try
{
if (!User.Exists(userEntity.EmailAddress))
{
var user = Membership.CreateUser(CreateUserName(userEntity), userEntity.Password,
userEntity.EmailAddress);
User scUser = User.FromName(userEntity.EmailAddress, true);
if (Role.Exists(Constants.Roles.ExampleRole) && !scUser.IsInRole(Constants.Roles.ExampleRole))
{
Roles.AddUserToRole(userEntity.EmailAddress, Constants.Roles.ExampleRole);
}
if (scUser != null)
{
using (new Sitecore.SecurityModel.SecurityDisabler())
{
scUser.Profile.FullName = userEntity.FirstName + " " + userEntity.LastName;
scUser.Profile.ProfileItemId = Constants.CustomUserProfile.ToString();
scUser.Profile.SetCustomProperty(UserFields.FirstName, userEntity.FirstName);
scUser.Profile.SetCustomProperty(UserFields.LastName, userEntity.LastName);
scUser.Profile.SetCustomProperty(UserFields.EmailAddress, userEntity.EmailAddress);
scUser.Profile.SetCustomProperty(UserFields.TelephoneNumber, userEntity.TelephoneNumber);
scUser.Profile.SetCustomProperty(UserFields.CompanyName, userEntity.CompanyName);
scUser.Profile.SetCustomProperty(UserFields.Sector, userEntity.Sector);
scUser.Profile.SetCustomProperty(UserFields.AddressLine1, userEntity.AddressLine1);
scUser.Profile.SetCustomProperty(UserFields.AddressLine2, userEntity.AddressLine2);
scUser.Profile.SetCustomProperty(UserFields.City, userEntity.City);
scUser.Profile.SetCustomProperty(UserFields.PostCode, userEntity.Postcode);
scUser.Profile.SetCustomProperty(UserFields.State, userEntity.State);
scUser.Profile.SetCustomProperty(UserFields.Country, CountryUtility.GetCountryNameById(userEntity.SelectedCountryId));
scUser.Profile.Save();
result = true;
}
Membership.UpdateUser(user);
Log.Info(scUser.Profile.FullName, new object());
Log.Info(scUser.Profile.ProfileItemId, new object());
Log.Info(scUser.Profile.GetCustomProperty(UserFields.FirstName), new object());
Log.Info(scUser.Profile.GetCustomProperty(UserFields.City), new object());
Log.Info(scUser.Profile.GetCustomProperty(UserFields.Country), new object());
Log.Info(scUser.IsInRole(Constants.Roles.ExampleRole) ? "true" : "false", new object());
}
}
else
{
throw new Exception(Nodes.Dictionary.Fields[DictionaryFields.RegistrationForm.UsernameExists].Value);
}
}
catch (Exception exception)
{
Log.Error(exception.Message, "RegistrationController");
}
我认为从 Sitecore 加载用户时需要使用完全限定的用户名,如下所示(假设 extranet
域):
User scUser = User.FromName("extranet\" + userEntity.EmailAddress, true);
在您的代码中似乎一切正常的原因是因为您从 User.FromName(...)
返回了一个用户,无论该用户是否实际存在。
我正在 Sitecore 站点 (Sitecore 7.0) 上构建注册组件,在创建用户的同时,none 正在创建自定义配置文件或角色。日志中没有错误。创建的用户具有正确的用户名和域,但没有自定义配置文件并且缺少全名、电子邮件地址、角色和其他自定义字段。有没有人在实现这个方面遇到过相关的困难?
N.b。使用电子邮件地址作为用户名,如果这会有什么不同的话。我已经更新了配置以允许这样做。我还在下面添加了一些日志记录,它们都输出了正确的信息。
这是代码示例:
try
{
if (!User.Exists(userEntity.EmailAddress))
{
var user = Membership.CreateUser(CreateUserName(userEntity), userEntity.Password,
userEntity.EmailAddress);
User scUser = User.FromName(userEntity.EmailAddress, true);
if (Role.Exists(Constants.Roles.ExampleRole) && !scUser.IsInRole(Constants.Roles.ExampleRole))
{
Roles.AddUserToRole(userEntity.EmailAddress, Constants.Roles.ExampleRole);
}
if (scUser != null)
{
using (new Sitecore.SecurityModel.SecurityDisabler())
{
scUser.Profile.FullName = userEntity.FirstName + " " + userEntity.LastName;
scUser.Profile.ProfileItemId = Constants.CustomUserProfile.ToString();
scUser.Profile.SetCustomProperty(UserFields.FirstName, userEntity.FirstName);
scUser.Profile.SetCustomProperty(UserFields.LastName, userEntity.LastName);
scUser.Profile.SetCustomProperty(UserFields.EmailAddress, userEntity.EmailAddress);
scUser.Profile.SetCustomProperty(UserFields.TelephoneNumber, userEntity.TelephoneNumber);
scUser.Profile.SetCustomProperty(UserFields.CompanyName, userEntity.CompanyName);
scUser.Profile.SetCustomProperty(UserFields.Sector, userEntity.Sector);
scUser.Profile.SetCustomProperty(UserFields.AddressLine1, userEntity.AddressLine1);
scUser.Profile.SetCustomProperty(UserFields.AddressLine2, userEntity.AddressLine2);
scUser.Profile.SetCustomProperty(UserFields.City, userEntity.City);
scUser.Profile.SetCustomProperty(UserFields.PostCode, userEntity.Postcode);
scUser.Profile.SetCustomProperty(UserFields.State, userEntity.State);
scUser.Profile.SetCustomProperty(UserFields.Country, CountryUtility.GetCountryNameById(userEntity.SelectedCountryId));
scUser.Profile.Save();
result = true;
}
Membership.UpdateUser(user);
Log.Info(scUser.Profile.FullName, new object());
Log.Info(scUser.Profile.ProfileItemId, new object());
Log.Info(scUser.Profile.GetCustomProperty(UserFields.FirstName), new object());
Log.Info(scUser.Profile.GetCustomProperty(UserFields.City), new object());
Log.Info(scUser.Profile.GetCustomProperty(UserFields.Country), new object());
Log.Info(scUser.IsInRole(Constants.Roles.ExampleRole) ? "true" : "false", new object());
}
}
else
{
throw new Exception(Nodes.Dictionary.Fields[DictionaryFields.RegistrationForm.UsernameExists].Value);
}
}
catch (Exception exception)
{
Log.Error(exception.Message, "RegistrationController");
}
我认为从 Sitecore 加载用户时需要使用完全限定的用户名,如下所示(假设 extranet
域):
User scUser = User.FromName("extranet\" + userEntity.EmailAddress, true);
在您的代码中似乎一切正常的原因是因为您从 User.FromName(...)
返回了一个用户,无论该用户是否实际存在。