API Questions on Kentico API.
Version 6.x > API > Problem in Create user API View modes: 
User avatar
Certified Developer v7
Certified  Developer v7
sachin-asentechllc - 3/5/2013 5:50:40 AM
   
Problem in Create user API
Hi,

I am creating a user using following code. i am using Kentico Ultimate Edition (v6.0.4297).
 UserInfo user = new UserInfo();
user = UserInfoProvider.GetUserInfo(userNameTextBox.Text.Trim());

if (user == null )
{
user = new UserInfo
{
UserName = userNameTextBox.Text.Trim(),
FirstName = userFirstNameTextBox.Text.Trim(),
LastName = userLastNameTextBox.Text.Trim(),
Email = userEmailAddressTextBox.Text.Trim(),
FullName = userFirstNameTextBox.Text.Trim() + " " + userLastNameTextBox.Text.Trim(),
IsEditor = false,
Enabled = true
};
//Custom field UserPhoneNumber
user.SetValue("UserPhoneNumber", txtPhoneNumberField.Text.Trim());

// Set Password
UserInfoProvider.SetPassword(user, password);

UserInfoProvider.SetUserInfo(user);
}

The problem is, Its not storing the password. When I checked the user in the SiteManager then it says the password is not set for the user.
Other fields are getting set properly.

Is it a correct way to create a user and set the pasword. if not then please guide.

Else please let me know the solution for it.

User avatar
Kentico Support
Kentico Support
kentico_radekm - 3/5/2013 6:16:43 AM
   
RE:Problem in Create user API
Hello.

Could you please try to set the password after the UserInfo object is created, i.e. after you call the UserInfoProvider.SetUserInfo(user) method? Does it help?

Best Regards,
Radek Macalik

User avatar
Certified Developer v7
Certified  Developer v7
sachin-asentechllc - 3/5/2013 6:29:14 AM
   
RE:Problem in Create user API
Hi.
I tried it but it is not working.

is there any other way?

Thanks and regards,
Sachin

User avatar
Kentico Consulting
Kentico Consulting
richards@kentico.com - 3/6/2013 1:36:24 AM
   
RE:Problem in Create user API
Hi,

Just a simple clarification - as I check our API you are using the SetPassword(UserInfo, String) which sets the password for the specified user object, saves the user object, correctly.

However you are trying to save "password" and I don't see from your code how did you get the password from user? I believe that you should also get it from some text box.

Is it possible that you have overlooked this?

Best regards,
Richard Sustek

User avatar
Certified Developer v7
Certified  Developer v7
sachin-asentechllc - 3/6/2013 5:22:19 AM
   
RE:Problem in Create user API
Hi Richards,

password is a string variable and I am storing the password in it. It contains a valid string value as I have tried displaying it to front end just before SetPassword(UserInfo, String) API call. Means the password is not blank.

I just want to clarify if i am moving in a right direction. if yes then why the password is not getting set.

Thanks,
Sachin

User avatar
Kentico Consulting
Kentico Consulting
richards@kentico.com - 3/6/2013 6:01:56 AM
   
RE:Problem in Create user API
Hi,

Could you please try following code? I have tested this and I was able to change the user password by using SetPassword(string, string) which takes a user name and a password as a parametre and saves it into database.

My code for that was:
CMS.SiteProvider.UserInfoProvider.SetPassword("UserName", "YourPassword")

Please let me know if that works.

Best regards,
Richard Sustek

User avatar
Certified Developer v7
Certified  Developer v7
sachin-asentechllc - 3/6/2013 7:19:54 AM
   
RE:Problem in Create user API
Hi Richards,

Where should I write the CMS.SiteProvider.UserInfoProvider.SetPassword("UserName", "YourPassword") statement?
After UserInfoProvider.SetUserInfo(user); line or before it?

Regards,
Sachin

User avatar
Kentico Consulting
Kentico Consulting
richards@kentico.com - 3/7/2013 3:22:00 AM
   
RE:Problem in Create user API
Hi Sachin,

I believe that it doesnt matter since you are not using an object.

This method SetPassword(string, string) simply takes the first argument as a UserName of a user and for that particular user sets the password in second argument.

E.G. My whole code for setting the password for Administrator would be:
CMS.SiteProvider.UserInfoProvider.SetPassword("Administrator", "SecretPassword");

And in your case it would probably be:
CMS.SiteProvider.UserInfoProvider.SetPassword(UserName, password);

And thats all, regardless of the placement.

Is it setting the password for you now?

Best regards,
Richard Sustek

User avatar
Certified Developer v7
Certified  Developer v7
sachin-asentechllc - 3/7/2013 1:44:18 PM
   
RE:Problem in Create user API
Hi Richards,

Thank you so much for you help. the solution
CMS.SiteProvider.UserInfoProvider.SetPassword("Administrator", "SecretPassword");

has really worked for me.

I have one more query. Is there any way if I could send an user account activation link via email to the registering user if he is disabled? I mean while registering using API if I keep user.Enabled=false then the user should get an email containing the user activation link.

i know if I set the Registration requires e-mail confirmation from site manager->settings->Security and Membership then the user will receive an email. But how can I send it after registering a user using Kentico API as I am coding.

Thanks and regards,
Sachin

User avatar
Kentico Consulting
Kentico Consulting
richards@kentico.com - 3/8/2013 1:05:28 AM
   
RE:Problem in Create user API
Hi Sachin,

Glad I was able to help.

May I ask why do you want to sent an email through API? I mean - we have a solid functionality build to do this so why not use it? If you are referring to that the e-mails are not send immediately - its probably because of the e-mail queue. It takes some time to send them and there is also task which sends e-mails so if you run this task the e-mails should be send.

Best regards,
Richard Sustek


User avatar
Certified Developer v7
Certified  Developer v7
sachin-asentechllc - 3/8/2013 6:17:08 AM
   
RE:Problem in Create user API
Hi Richards,

As Iam using a custom form and custom coding for registration, I want to disable the user at first time. Then a mail will be sent to the registering user with an activation link. If user clicks on that link then his account will be activated.

So is this sending activation link through e-mail is an built in functionality or I have to do any configuration for it?

Thanks and regards,
Sachin

User avatar
Kentico Support
Kentico Support
kentico_radekm - 3/9/2013 2:17:46 AM
   
RE:Problem in Create user API
Hello.

I believe we have built-in functionality for this. In the Site Manager / Settings / Security & Membership section you can enable the "Registration requires e-mail confirmation" setting.

Best Regards,
Radek Macalik