Creating a new member programmatically in Umbraco

I am trying to create a new member for my Umbraco website programmatically, but I'm not sure if I am doing it right.

My code is as follows:

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(); 

But when I run my code, I do not see anything that appears in my Umbraco. Can someone please tell me what I did wrong?

+6
source share
3 answers

If you are using umbraco 7, it is best to use a member service. Below is a simple approach that you could use to achieve this.

 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); } 
+9
source

The object is pretty much in demand, but the following code is for your model and controller, which should put you on the right track. Hope you know enough about MVC to achieve this.

Your model may contain something like the following and fill in with your view

  using System.ComponentModel.DataAnnotations; using System.Web; namespace MyProject.Models { public class MemberModel { [Required] public string Name { get; set; } [Required] [EmailAddress] public string Email { get; set; } [Required] public string Password { get; set; } } } 

Your controller might look something like this:

 using System.Web.Mvc; using MyProject.Models; using Umbraco.Web.Mvc; namespace MyProject.Controllers { public class MemberController : SurfaceController { public ActionResult SignUp(MemberModel model) { if (!ModelState.IsValid) return CurrentUmbracoPage(); var memberService = Services.MemberService; if (memberService.GetByEmail(model.Email) != null) { ModelState.AddModelError("", "Member already exists"); return CurrentUmbracoPage(); } var member = memberService.CreateMemberWithIdentity(model.Email, model.Email, model.Name, "MyMemberType"); memberService.Save(member); memberService.SavePassword(member,model.Password); Members.Login(model.Email, model.Password); return Redirect("/"); } } } 
+3
source

Depending on the version of Umbraco, the sample code shown is out of date.

Umbraco 4 has changed the Umbraco membership model to use the ASP.NET Membership Provider Model, which means that all abstract classes that are provided by Out-Of-The-Box with ASP.NET are able to access the Umbraco Member. There are many good resources (and other sites) in MSDN for using ASP.NET membership, the recommended starting point is here.

Articles:

http://our.umbraco.org/wiki/how-tos/membership-providers

http://msdn.microsoft.com/en-us/library/tw292whz.aspx

0
source

Source: https://habr.com/ru/post/980655/


All Articles