我正在Sitecore站点(Sitecore 7.0)上构建注册组件,并且在创建用户时,没有创建自定义配置文件或角色。日志中没有错误。创建的用户具有正确的用户名和域,但没有自定义配置文件,并且缺少全名,电子邮件地址,角色和其他自定义字段。有没有人遇到任何相关的困难实现这个?
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");
}
答案 0 :(得分:2)
我认为在从Sitecore加载用户时需要使用完全限定的用户名,如下所示(假设extranet
域名):
User scUser = User.FromName("extranet\\" + userEntity.EmailAddress, true);
一切似乎在您的代码中起作用的原因是因为您从用户实际存在的User.FromName(...)
返回用户。