Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tarunjit 103 posts 146 karma points
    Apr 29, 2020 @ 08:59
    Tarunjit
    0

    Create Umbraco User Programatically

    Hi All,

    We need to create umbraco user programatically through C# (Scheduled task/Window Service) in already existing Umbraco Data Base.

    Please help.

    Regards

    Tarunjit Singh

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Apr 29, 2020 @ 12:54
    Alex Skrypnyk
    0

    Hi

    This code works for me:

    var userService = ApplicationContext.Current.Services.UserService;
    
    var email = "youremailhere";
    var date = DateTime.Now;
    var userType = new ProvisioningUserType()
    {
        Alias = "admin",
        Name = "Administrators",
        CreateDate = date,
    };
    var user = userService.GetByEmail(email);
    userService.SaveUserType(userType);
    if (user == null) {
        // Create a new user
        var newUser = userService.CreateUserWithIdentity(email, email, userType);
        newUser.RawPasswordValue = (Membership.Providers["UsersMembershipProvider"] as UsersMembershipProvider).HashPasswordForStorage("password"); ;
        // Save the new user
        userService.Save(newUser);
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft