Copied to clipboard

Flag this post as spam?

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


  • Jin Botol 134 posts 287 karma points
    Jan 12, 2017 @ 03:17
    Jin Botol
    0

    How to add member to the specific Member Type

    Hi everyone,

    I installed package (Install-Package UmbracoIdentity), all are works so fine. But I want to add the user to my created Member Type called ("Social").

    Here's my code.

                var user = new UmbracoApplicationMember()
                {
                    Name = info.ExternalIdentity.Name,
                    UserName = model.Email,
                    Email = model.Email
                };
    
                var externalMemberService = Services.MemberService;
    
                if (externalMemberService.GetByEmail(model.Email) == null)
                {
                    var socialMember = externalMemberService.CreateMember(user.UserName, user.Email, user.Name, "Social");
    
                    Roles.AddUserToRole(socialMember.Username, "Social");
    
                    socialMember.SetValue("profileName", user.Name);
    
                    externalMemberService.Save(socialMember);
    
                }
    
                var result = await UserManager.CreateAsync(user);
    
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddModelErrors(result);
    

    But I got an error enter image description here

    Can anyone help me, I'm new in Umbraco also in ASP and I'm stuck.

    Any help will be appreciated. Thanks in advance.

    Regards,

    Jin

  • Jin Botol 134 posts 287 karma points
    Jan 12, 2017 @ 03:29
    Jin Botol
    0

    I tried to delete this line

    Roles.AddUserToRole(socialMember.Username, "Social");
    

    the error has gone but the page isn't redirecting and it always says

    Name [email protected] is already taken.

    Email '[email protected]' is already taken.

    even if that is the first email I register, and also I tried to change the email, it's always says

    Name... is already taken. Email.... is already taken

    Am I doing right, or I got missing code?

    Regards,

    Jin

  • Phill 115 posts 289 karma points
    Jan 12, 2017 @ 17:46
    Phill
    0

    Hi Jin,

    I do this with the following line. Note that Member Type and Roles are different. With the following code you can create a member specifying username, email, password and they MemberType.

    var newMember = Services.MemberService.CreateWithIdentity("uniqueUserName", "[email protected]", "defaultpassword", "Social");
    

    I should also add that you may want to check to see if user exists prior to trying to create then you can run create or update code. So something like:

    var memberExists = Services.MemberService.Exists("[email protected]");
                                            if (memberExists)
                                            {
    

    // create new member code here... }

  • John Bergman 483 posts 1132 karma points
    Jan 12, 2017 @ 17:26
    John Bergman
    0

    Where did you find the package "Install-Package UmbracoIdentity", I am looking to do something similar, maybe I can help figure this out

  • Phill 115 posts 289 karma points
    Jan 12, 2017 @ 17:47
    Phill
    1

    Hi John, I can't say for sure, but Jin might be referring to the following:

    https://github.com/Shazwazza/UmbracoIdentity

  • Jin Botol 134 posts 287 karma points
    Jan 13, 2017 @ 02:12
    Jin Botol
    0

    Hi John,

    Yes, Phill is right,

  • John Bergman 483 posts 1132 karma points
    Jan 12, 2017 @ 17:54
    John Bergman
    0

    Thanks Phil, I bet you are right

  • Jin Botol 134 posts 287 karma points
    Jan 26, 2017 @ 08:25
    Jin Botol
    0

    Help me, I'm stuck

  • Jin Botol 134 posts 287 karma points
    Jan 30, 2017 @ 02:03
    Jin Botol
    0

    Anyone can help me?

  • John Bergman 483 posts 1132 karma points
    Jan 30, 2017 @ 02:30
    John Bergman
    0

    Try something like this

    var userToSave = AppHelpers.UmbMemberHelper().CreateRegistrationModel(AppConstants.MemberTypeAlias);
    userToSave.Name = userToSave.Username;
    userToSave.UsernameIsEmail = false;
    userToSave.Email = userModel.Email;
    userToSave.Password = userModel.Password;
    
    MembershipCreateStatus createStatus;
    AppHelpers.UmbMemberHelper().RegisterMember(userToSave, out createStatus, false);
    
    if (createStatus != MembershipCreateStatus.Success)
    {
    
       // Failed
    
    }
    else 
    {
    
       // re-get the umbraco member, so we pick up anything that was added during the save
       var umbracoMember = AppHelpers.UmbServices().MemberService.GetByUsername(userToSave.Username);
    
       // Set the role/group they should be in
       AppHelpers.UmbServices().MemberService.AssignRole(umbracoMember.Id, newMemberGroup.Name);
    
    }
    

    (Sorry couldn't get the code formatting to work). This is what I use on Umbraco 7.5x

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Jan 30, 2017 @ 07:43
    Sebastiaan Janssen
    0

    Formatting fixed. For future reference, there's a button ;)

    enter image description here

  • John Bergman 483 posts 1132 karma points
    Jan 30, 2017 @ 07:45
    John Bergman
    0

    I tried that, it kept only formatting a small subset of it and then injected a horizontal scroll bar and turned the rest to text.

    (Thanks for fixing the formatting)

Please Sign in or register to post replies

Write your reply to:

Draft