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



我正在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(...)中获得用户。

最新更新