Copied to clipboard

Flag this post as spam?

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


  • Tarunjit 99 posts 142 karma points
    28 days ago
    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 5730 posts 21691 karma points MVP 3x admin c-trib
    28 days ago
    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