Dealing with IUser programmatically

Topics: General, Troubleshooting
Apr 18, 2011 at 1:27 AM

Once I get the user, how can I change the password? Is it possible?

                var adminUser = (from u in connection.Get<IUser>()
                             where u.Username == "admin"
                             select u).FirstOrDefault();

I'm guessing this is where I start.

Apr 18, 2011 at 1:42 AM

adminUser.EncryptedPassword = "something".Encrypt(); connection.Update(adminUser);

Where Encrypt is a extension-method in the Composite.C1Console.Security.Cryptography namespace

You shouldn't do it this way though, since

  1. passwords are not guaranteed to be validated against a IUser instance
  2. Composite has a Facade class for updating the password

So you do like this instead

UserValidationFacade.FormSetUserPassword("admin", "something");

Apr 18, 2011 at 1:48 AM

You can also use the newly added MembershipProvider in the Contrib project which will abstract away the inner workings of C1 and let you deal with the users in a pure way. Using MembershipProvider the method for updating a password would be like this

var user = Membership.GetUser("admin");
user.ChangePassword("oldPassword", "newPassword");
See more on the MembershipProvider here

Apr 18, 2011 at 9:02 AM

That's perfect burning ice. I was looking for a security class like that in the api, but couldn't find it: UserValidationFacade.

I'm also going to look into the MembershipProvider Class

Apr 18, 2011 at 1:12 PM
Edited Apr 18, 2011 at 1:14 PM

Yeah, unfortunately many of the more "advance" and specialized api's requires digging around in the source code to find... but I've done that so i'm just happy to share my findings :)

Also note, that if the LoginProvider (in App_Data/Composite/Composite.config) is changed to one that doesn't support updating a users password the above method will fail. You can therefor check for the property UserValidationFacade.CanSetUserPassword before you make the call.