我正在尝试使用Sitecore规则将用户添加到特定角色。我正在考虑在/ Actions / New User Action项目中的Items Saved事件中编写代码,但我不确定要写什么。甚至可以通过规则将用户添加到角色中吗?如果是这样,我该如何使用类型,代码,参考和语言字段?
答案 0 :(得分:0)
使用以下代码创建程序集,并在app_include中的["事件名称="项目:已保存"]处理程序中调用程序集或设置" Namespace.AddNewUser.AddUser,yourAssembly& #34;在类型字段中。
public void AddUser(string domain, string firstName, string lastName, string email,
string comment, string telephoneNumber, string jobTitle)
{
string userName = string.Concat(firstName, lastName);
userName = string.Format(@"{0}\{1}", domain, userName);
// You can manually set the password or from Sitecore's generator
string newPassword = Membership.GeneratePassword(10, 3);
try
{
if (!User.Exists(userName))
{
Membership.CreateUser(userName, newPassword, email);
// Edit the profile information
Sitecore.Security.Accounts.User user = Sitecore.Security.Accounts.User.FromName(userName, true);
Sitecore.Security.UserProfile userProfile = user.Profile;
userProfile.FullName = string.Format("{0} {1}", firstName, lastName);
userProfile.Comment = comment;
// Assigning the user profile template
userProfile.SetPropertyValue("ProfileItemId", "{this is profileItem's GUID}");
// Have modified the user template to also contain telephone number and job title.
userProfile.SetCustomProperty("TelephoneNumber", telephoneNumber);
userProfile.SetCustomProperty("JobTitle", jobTitle);
userProfile.Save();
}
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error(string.Format("Error in Client.Project.Security.UserMaintenance (AddUser): Message: {0}; Source:{1}", ex.Message, ex.Source), this);
}
}