Skip to content

Instantly share code, notes, and snippets.

@technocake
Created October 21, 2021 09:19
Show Gist options
  • Save technocake/7696b94ea412187389b4855b3c149e94 to your computer and use it in GitHub Desktop.
Save technocake/7696b94ea412187389b4855b3c149e94 to your computer and use it in GitHub Desktop.
umbraco-programatically-user
// in top of view file
@using Umbraco.Core;
@using Umbraco.Core.Models;
@using Umbraco.Core.Models.Membership;
@using Umbraco.Core.Services;
@using Umbraco.Web.Security.Providers;
@{
// set email and Name here
var email = "<email>";
var username = email;
var password = "HelloWorld1234";
var name = "<Name>";
// setup
var userType = "admin";
var group = Services.UserService.GetUserGroupByAlias("admin") as IReadOnlyUserGroup;
var userService = Services.UserService;
// Create user
var user = userService.CreateWithIdentity(email, email, name, userType);
user.AddGroup(group);
var hashedPassword = (Membership.Providers["UsersMembershipProvider"] as UsersMembershipProvider).HashPasswordForStorage(password);
user.RawPasswordValue = hashedPassword;
userService.Save(user);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment