我正在尝试以编程方式为我的Umbraco网站创建一个新成员,但我不确定我是否正确地执行此操作.
我的代码看起来像这样:
MemberType demoMemberType = new MemberType(1040); //id of membertype ‘demo’ Member newMember = Member.MakeNew(newEmployee.FirstName + " " + newEmployee.LastName,demoMemberType,new umbraco.BusinessLogic.User(0)); newMember.Email = "test@testmail.com"; newMember.Password = "password"; newMember.LoginName = "Test"; newMember.getProperty("firstName").Value = "test"; newMember.Save();
但是当我运行我的代码时,我看不到任何出现在我的Umbraco中的东西.有人可以告诉我我做错了什么吗?
解决方法
如果您使用的是umbraco 7,最好使用会员服务.以下是您可以采用的简单方法来实现此目的.
public int RegisterMember(string memberName,string emailAddress,string memberPassword,string memberTypeAlias,string memberGroupName) { int umbracoMemberId = -1; if (!MemberExists(emailAddress)) { IMember newMember = ApplicationContext.Current.Services.MemberService.CreateMember(emailAddress,emailAddress,memberName,memberTypeAlias); try { ApplicationContext.Current.Services.MemberService.Save(newMember); ApplicationContext.Current.Services.MemberService.SavePassword(newMember,memberPassword); ApplicationContext.Current.Services.MemberService.AssignRole(newMember.Id,memberGroupName); umbracoMemberId = newMember.Id; } catch (Exception ex) { throw new Exception("Unable to create new member " + ex.Message); } } return umbracoMemberId; } public bool MemberExists(string emailAddress) { return (ApplicationContext.Current.Services.MemberService.GetByEmail(emailAddress) != null); }