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

HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0Creating User and Assign Role programatically using C# in dnnCreating User and Assign Role programatically using C# in dnn
Previous
 
Next
New Post
11/11/2014 5:41 AM
 

Hi All,

Im trying to creating user and assign them to a role in dnn using c#, what i have keeps throwing object reference exception and un-enable to create the user. The Role that i need to assign the user is coming from a query string. when ever i execute the code, it doesnt insert into the user table and its gives the error below. Please i need your help if there is something i'm missing.

 

Thanks

--------------------ERROR--------------

Error: Manage is currently unavailable. DotNetNuke.Services.Exceptions.ModuleLoadException: Object reference not set to an instance of an object. ---> System.NullReferenceException: Object reference not set to an instance of an object. at DotNetNuke.Security.Roles.RoleController.AddUserRole(Int32 portalId, Int32 userId, Int32 roleId, RoleStatus status, Boolean isOwner, DateTime effectiveDate, DateTime expiryDate) at DotNetNuke.Security.Roles.RoleController.AddUserRole(Int32 portalId, Int32 userId, Int32 roleId, DateTime effectiveDate, DateTime expiryDate) at Christoc.Modules.ProjectName.ManageCTSO.ManageTSOMsg.Page_Load(Object sender, EventArgs e) in c:\dnndev.me\DesktopModules\ProjectName\ManageCTSO\ManageTSOMsg.ascx.cs:line 87 --- End of inner exception stack trace ---

---------------------------------------- 

 

                     string space = " ";

                      UserInfo oUser = new UserInfo();

                      oUser.PortalID = this.PortalId;

                      oUser.IsSuperUser = false;

                      oUser.FirstName = Session["fname"].ToString();

                      oUser.LastName = Session["lname"].ToString();

                      oUser.Email = Session["email"].ToString();

                      oUser.Username = Session["username"].ToString();

                      oUser.DisplayName = Session["fname"].ToString() + space.ToString() + Session["lname"].ToString();

                      //Fill MINIMUM Profile Items (KEY PIECE)

                      oUser.Profile.PreferredLocale = PortalSettings.DefaultLanguage;

                      // oUser.Profile.PreferredTimeZone =DateTime.Parse(PortalSettings.TimeZoneOffset);

                      oUser.Profile.FirstName = oUser.FirstName;

                      oUser.Profile.LastName = oUser.LastName;

                      //Set Membership:

                      UserMembership oNewMembership = new UserMembership();

                      oNewMembership.Approved = true;

                      oNewMembership.CreatedDate = System.DateTime.Now;

                      oNewMembership.Email = oUser.Email;

                      oNewMembership.IsOnLine = false;

                      oNewMembership.Username = oUser.Username;

                      oNewMembership.Password = UserController.GeneratePassword(8);

                      oUser.Membership = oNewMembership;

                     if (UserCreateStatus.Success == UserController.CreateUser(ref oUser))

                      {

                          RoleController oDnnRoleController = new RoleController();

                         //Get the role information

                          RoleInfo oCurrentRole = oDnnRoleController.GetRoleByName(this.PortalId,                             Request.QueryString["TSORole"].ToString());

                          // RoleInfo oCurrentRole1 = oDnnRoleController.GetRoleByName(this.PortalId, " Subscribers");

                          //Assign to user

                          oDnnRoleController.AddUserRole(this.PortalId, oUser.UserID, oCurrentRole.RoleID, Null.NullDate, Null.NullDate);

                          oDnnRoleController.DeleteUserRole(this.PortalId, int.Parse(oUser.UserID.ToString()), oCurrentRole.RoleID);

                         }

 
New Post
11/11/2014 8:34 AM
 
Profile object is part of membership (and needs to be created) and you might need an initialization method for membership.
And last but not least, I would expect that AddUserRole requires the user to be persisted in the database.

Cheers from Germany,
Sebastian Leupold (Microsoft MVP)

dnnWerk - The DotNetNuke Experts   German Spoken DotNetNuke User Group

Speed up your DNN Websites with TurboDNN
 
Previous
 
Next
HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0Creating User and Assign Role programatically using C# in dnnCreating User and Assign Role programatically using C# in dnn


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.

Content Layout

Subscribe to DNN Digest

Subscribe to DNN Digest

DNN Digest is our monthly email newsletter. It highlights news and content from around the DNN ecosystem, such as new modules and themes, messages from leadership, blog posts and notable tweets. Keep your finger on the pulse of the ecosystem by subscribing.  


Copyright 2017 by DNN Corp Terms of Use Privacy
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out