Skip to content

Instantly share code, notes, and snippets.

@VesselinVassilev
Created June 27, 2018 11:11
Show Gist options
  • Select an option

  • Save VesselinVassilev/d1d487614303b5532fdc6d2d20808f77 to your computer and use it in GitHub Desktop.

Select an option

Save VesselinVassilev/d1d487614303b5532fdc6d2d20808f77 to your computer and use it in GitHub Desktop.
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