Created
October 21, 2021 09:19
-
-
Save technocake/7696b94ea412187389b4855b3c149e94 to your computer and use it in GitHub Desktop.
umbraco-programatically-user
This file contains 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
// 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