gpt4 book ai didi

asp.net - 以编程方式创建时未保存 Sitecore 用户配置文件设置

转载 作者:行者123 更新时间:2023-12-02 14:18:42 31 4
gpt4 key购买 nike

我正在 Sitecore 站点 (Sitecore 7.0) 上构建注册组件,在创建用户时,不会创建任何自定义配置文件或角色。日志中没有错误。创建的用户具有正确的用户名和域,但没有自定义配置文件,并且缺少全名、电子邮件地址、角色和其他自定义字段。有没有人遇到任何相关的困难来实现这一点?

注意使用电子邮件地址作为用户名(如果这会产生任何影响)。我已经更新了配置以允许这样做。我还在下面添加了一些日志记录,它们都输出正确的信息。

这是代码示例:

 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(...) 返回了一个用户,无论该用户是否实际存在。

关于asp.net - 以编程方式创建时未保存 Sitecore 用户配置文件设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33655544/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com