Created
June 27, 2018 11:11
-
-
Save VesselinVassilev/d1d487614303b5532fdc6d2d20808f77 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static Guid CreateSitefinityUser(AgentModel model, UserRole userRole) | |
| { | |
| var uManager = UserManager.GetManager(); | |
| var userId = Guid.NewGuid(); | |
| try | |
| { | |
| using (new ElevatedModeRegion(uManager)) | |
| { | |
| if (model.Username.IsNullOrEmpty()) | |
| { | |
| model.Username = model.EmailAddress; | |
| } | |
| var user = uManager.CreateUser(model.Username, model.Password, model.EmailAddress, string.Empty, string.Empty, true, userId, out MembershipCreateStatus status); | |
| if (status != MembershipCreateStatus.Success) | |
| { | |
| throw new Exception("Can't create user: " + status.ToString()); | |
| } | |
| // create profile | |
| UserProfileManager pManager = UserProfileManager.GetManager(); | |
| using (new ElevatedModeRegion(pManager)) | |
| { | |
| SitefinityProfile profile = pManager.CreateProfile(user, Guid.NewGuid(), typeof(SitefinityProfile)) as SitefinityProfile; | |
| profile.FirstName = model.FirstName; | |
| profile.LastName = model.LastName; | |
| profile.Nickname = model.Username; | |
| using (CultureRegion cultureRegion = new CultureRegion(CultureInfo.InvariantCulture)) | |
| { | |
| pManager.RecompileItemUrls<SitefinityProfile>(profile); | |
| } | |
| pManager.SaveChanges(); | |
| } | |
| // add user to either Agent or Affiliate role | |
| var rManager = RoleManager.GetManager(); | |
| Role role = rManager.GetRole(userRole.ToString()); | |
| using (new ElevatedModeRegion(rManager)) | |
| { | |
| rManager.AddUserToRole(user, role); | |
| rManager.SaveChanges(); | |
| } | |
| uManager.SaveChanges(); | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| Log.Write(ex); | |
| throw; | |
| } | |
| return userId; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment