This project is read-only.

Creating User in IBackgroundTask Sweep()

Topics: Customizing Orchard, Troubleshooting, Writing modules
Apr 12, 2011 at 6:04 AM


I have managed to create a BackgroundTask that connects via webservices to an external system, checks for new users, and runs code that should create a user in Orchard.

Only, the code runs but the user is not created.  The code below runs, the var user is not null,  the setting of roles executes and no exception occurs.


I saw one similar discussion but the only difference I can see is that they login the user.  

Is there some call in the membership service or Orchard framework that I am missing?  Do I have to login the user to commit the changes to the DB


My code within Sweep() is 

		    string baseName = Case.AssignedToFirstName + Case.AssignedToLastName;
                    string password = Case.CaseNumber.ToString();
                    string userName = baseName; 
                    int i = 0;
                    while (_membershipService.GetUser(userName) != null)
                        userName = baseName + i.ToString();

                    var user = _membershipService.CreateUser(new CreateUserParams(userName, Case.CaseNumber.ToString(), Case.AssignedToEmail, null, null, true));

                    if (user != null)
                        List<string> roles = new List<string>() { "Contributor" };
                        UpdateUserRoles(user, roles);






Apr 13, 2011 at 12:36 AM

For background threads using the content manager, you need to authenticate using a specific account like the super user account (CurrentSite.SuperUser), and the IAuthenticationService.SetAuthenticatedUserForRequest. Because updated content if it contains a Common part, will update the author.