Copied to clipboard

Flag this post as spam?

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


  • Alex 35 posts 55 karma points
    Oct 25, 2010 @ 09:12
    Alex
    0

    How do I create users from c# code?

    I can create users like this:

    umbraco.BusinessLogic.User.MakeNew("name", "login", "password", myUserType);

    But these users cannot login into Umbraco CMS then. I have checked the umbracoUser table and I see that passwords for users created manually (from UI) are encrypted/hashed. Passwords for the users I create from C# code are stored in clear form and that is why they cannot login I guess. What else should I do to create a user?

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Oct 25, 2010 @ 09:44
    Matt Brailsford
    2

    Hi Alex,

    You'll probably want to use the Users Membership provider to create the users, as this already contains the logic to hash the passwords for you, so something like:

     Membership.Providers["UsersMembershipProvider"].CreateUser(string username, string password, string email, ...)

    Many thanks

    Matt

    PS, You'll notice I get the provider by name, as if you look in the web.config, you'll see the Members membership provider is the default, so we need to specifically say we want to use the Users provider

     

  • Alex 35 posts 55 karma points
    Oct 25, 2010 @ 10:08
    Alex
    0

    It does not work either. Internally it calls             User.MakeNew(username, username, password, userType);

    I guess I need to call provider.EncodePassword(string password) first
  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Oct 25, 2010 @ 10:23
    Matt Brailsford
    0

    Hi Alex,

    It looks like you are right. Looking at the source, it does look like it just saves the value straight to the DB, so yes, you will need to call EncodePassword before hand. It might be worth checking the source for the creat user dialog to see how Umbraco are doing it.

    It would certainly make sence though that this was part of the CreateUser method of the Membership provider though, so might be worth checking codeplex to see if it has been raised as an issue, and if not, add it.

    Matt

Please Sign in or register to post replies

Write your reply to:

Draft