Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Miguel Lopez 15 posts 117 karma points
    Jun 05, 2019 @ 09:46
    Miguel Lopez
    1

    Programmatically create user with password in V8

    I am trying to create a user programmatically with a given password in V8. The code is as follows, with the two options I have tested with no luck:

    var user = userService.CreateUserWithIdentity(email, email); 
    user.Name = name;
    user.RawPasswordValue = password;
    userService.Save(user);
    

    or:

    var user = userService.CreateWithIdentity(email, email, password, "guest", true);
    user.Name = name;
    userService.Save(user);
    

    None of these methods is saving the right password though, as it is not hashed when saved in the DB and the user cannot log in then. In V7 the userService had a method like userService.SavePassword(user,password), but I cannot seem to find it anymore in V8.

    Thanks.

  • Tom Madden 252 posts 454 karma points MVP 4x c-trib
    Jun 20, 2019 @ 09:10
    Tom Madden
    0

    Hi,

    I believe SavePassword is now renamed as ChangePassword

    regards

    t

  • Miguel Lopez 15 posts 117 karma points
    Jun 21, 2019 @ 09:32
    Miguel Lopez
    0

    Thanks Tom,

    it seems the UserService doesn't have any of those methods, either ChangePassword or SavePassword.

  • Tarik | WPPlumber 179 posts 801 karma points c-trib
    Jun 21, 2019 @ 11:06
    Tarik | WPPlumber
    0

    I found Umbraco V8 using ChangePasswordWithIdentityAsync when you edit user profile from the backoffice.

    I suggest to see the issue mentioned about this subject.

  • George Phillipson 108 posts 287 karma points
    Jun 16, 2020 @ 15:28
    George Phillipson
    0

    I know this is an old post, but this is how I do it.

    var memberDetails = memberService.CreateMemberWithIdentity(model.Username, model.Email, $"{model.FirstName} {model.LastName}", "member");
                        memberDetails.SetValue("firstName", model.FirstName);
                        memberDetails.SetValue("lastName", model.LastName);
                        memberDetails.SetValue("address", model.Address);
                        memberDetails.SetValue("city", model.City);
                        memberDetails.SetValue("county", model.County);
                        memberDetails.SetValue("postCode", model.PostCode);
                        memberDetails.SetValue("contactNumber", model.ContactNumber);
                        memberDetails.IsApproved = false;//Change to true if you want to allow the member instant access, or false if they need to verify email etc before they can login
                        memberService.AssignRole(memberId: memberDetails.Id, roleName: "Member"); // I always assign the default role
                        memberService.SavePassword(memberDetails, model.Password);
    
                        memberService.Save(memberDetails);
    

    And don't forget to change 'allowManuallyChangingPassword' in web.config to true.

    I also found in two projects that the members could not create an account, turned out that these this setting minRequiredPasswordLength="10" was overriding my custom password length. Why it only happened on two websites I don't know; I never had time to investigate due to time constraints.

    Hope it helps

Please Sign in or register to post replies

Write your reply to:

Draft