Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDevelopment and...Development and...DNN Platform (o...DNN Platform (o...Programmatically creating user and adding to role with DNN APIProgrammatically creating user and adding to role with DNN API
Previous
 
Next
New Post
6/24/2015 10:36 AM
 

I've had great success creating a user with DNN's UserController but I cannot seem to get it to add the user to a role on create. I don't get an error either. Most of the user and role threads I have found appear to be obsolete. 

Anyone know something I don't?

        private UserInfo GenerateUserInfo(WholesaleClient client)

        {

            var userInfo = new UserInfo();

            userInfo.Username = client.Username;

            //set role - not working

            userInfo.Roles = new string[] { "Wholesale User" }; 

            

            //set auto generated password

            userInfo.Membership.Password = membershipProvider.GeneratePassword();

            userInfo.Email = client.Email;

            userInfo.FirstName = client.FirstName;

            userInfo.LastName = client.LastName;

            userInfo.Username = client.Username;

            userInfo.DisplayName = client.BusinessName;

            userInfo.PortalID = 0;        

 

            return userInfo;

        }

 
New Post
6/24/2015 4:10 PM
Accepted Answer 

Buy the new Professional DNN7: Open Source .NET CMS Platform book Amazon US
 
New Post
6/24/2015 4:54 PM
 

Thanks Cathal. I did come across that but I'm not very familiar with VB, however, looking at it again I  am able to surmise that it first creates the user, then grabs the userID which is used to add the user to the role if the user creation was successful. I'll try it out... hopefully I can adapt that in C# and the way I have to implement this.  

I don't mean to be a pain but I really hope DNN improves on their API documentation...the most recent only displays the namespaces, no examples. DNN is a great framework from what I have seen but where is MVC, RECENT module development tutorials and ORM support? In terms of Visual Studio templates I've only found very old extensions. Being, albeit extended, family of Microsoft I would have thought DNN development would be a lot smoother and perhaps with even more best practice orientated methodologies. Hope to see that change. 

 
New Post
6/25/2015 3:30 AM
 

I tried referring to the VB code for adding a user but picked up a few obsolete warnings.

Where I'm currently stuck:

I'll need to grab that userID for the user that was just created, so I'll probably end up doing that in the if(createStatus == UserCreateStatus.Success) block. Then I'd have to add the user to the role with the RoleController. I don't want auto-assignment, I know the role I'd like the user added to. 

I'm struggling to find any recent examples in c# for this.

 
New Post
6/25/2015 7:28 AM
 

Came right with this:

AddRole method:

public bool addRoleToUser(UserInfo user, string roleName, DateTime expiry)

        {

       bool rc = false;

       RoleInfo newRole = roleController.GetRoleByName(user.PortalID, roleName);

       if (newRole != null && user != null)

       {

       rc = user.IsInRole(roleName);

                roleController.AddUserRole(user.PortalID, user.UserID, newRole.RoleID, DateTime.MinValue, expiry);

       

       user = UserController.GetUserById(user.PortalID, user.UserID);

       rc = user.IsInRole(roleName);

       }

       return rc;

        }

Create user and add to role:

private void ApproveFor(WholesaleClient client)

        {

            client.IsVerified = true;

            var userInfo = GenerateUserInfo(client);

            var createStatus = membershipProvider.CreateUser(ref userInfo);

 

            if (createStatus == UserCreateStatus.InvalidPassword)

                throw new Exception("Invalid password");

 

            //save back to table with IsVerified set

            if (createStatus == UserCreateStatus.Success)

            {

                var roleName = "Wholesale Client";

                addRoleToUser(userInfo, roleName, DateTime.MaxValue);

                clientRepository.Update(client);

                return;   

            }

 

            throw new Exception("Error occurred");

 

        }

 
Previous
 
Next
HomeHomeDevelopment and...Development and...DNN Platform (o...DNN Platform (o...Programmatically creating user and adding to role with DNN APIProgrammatically creating user and adding to role with DNN API


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out