Authenticate user via the API

a t asked on May 19, 2014 14:53

I am very new to Kentico and would really appreciate some help.

In the project that I am working on (we are using Kentico 7), we are allowing users to be able to change their usernames(email). The problem is, whenever the user changes his/her username, he.she is then logged out an need to log in again.

I would like to avoid this and authenticate the user via the API after the email is saved. How do I do this?

Thanks so much in advance!

Correct Answer

Brenden Kehren answered on May 20, 2014 08:11

Try this for v7 (you might also want to put a check in place to see if the username really has changed):

UserInfo userInfoToChange = UserInfoProvider.GetUserInfo(userId); 
userInfoToChange.UserName = "a@t.com";
UserInfoProvider.SetUserInfo(userInfoToChange);

// Ensures the current user is not logged out if their username changes
if (RequestHelper.IsFormsAuthentication())
{
    FormsAuthentication.SetAuthCookie(userInfoToChange.UserName, false);
    CMSContext.CurrentUser = new CurrentUserInfo(userInfoToChange, true);
}
4 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on May 19, 2014 16:24

Take a look at the LoginForm.ascx webpart control in /CMSWebParts/Membership/Logon/ directory of your website. You should be able to get info there on authenticating a user based on a username.

0 votesVote for this answer Mark as a Correct answer

a t answered on May 19, 2014 19:09

Brenden,

Thanks so much for the quick reply. I have the simple code below as a POC and what happens is that the user is logged out as soon as the call to "UserInfoProvider.SetUserInfo(userInfoToChange);" is called.

UserInfo userInfoToChange = UserInfoProvider.GetUserInfo(userId) userInfoToChange.UserName = "a@t.com";

UserInfoProvider.SetUserInfo(userInfoToChange);

UserInfo userInfo = UserInfoProvider.GetUserInfoForSitePrefix("a@t.com", CMSContext.CurrentSite); CMSContext.AuthenticateUser(userInfo.UserName, false);

Is there some setting I need to set? What am I missing?

Thank you so much in advance!

0 votesVote for this answer Mark as a Correct answer

a t answered on May 20, 2014 09:51

Thanks so much that did it!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.