How to make user logged in from code in Kentico 9?

291 Views Asked by At

In kentico 9 application, created user from code and assigned role and added into current site.

User is successfully created in kentico database and added into site, and called kentico API to authenticate newly created user and its returning true. After that I am redirecting end user on site home page and on master template used current user webpart which is not reflecting to show user as logged in.

Please suggest.

2

There are 2 best solutions below

0
On

I suspect you are not authenticating the user properly. Try using following code to authenticate user:

 var userName = "Arnold";
 AuthenticationHelper.AuthenticateUser(userName, true);

Note: Depending on where you use this code, you might need to do a redirect using

 URLHelper.Redirect("url");
0
On

You can use the AuthenticationHelper.AuthenticateUser method to authenticate the user.

void AuthenticateUser(string userName, bool createPersistentCookie, bool loadCultures = true)

The createPersistentCookie parameter is basically the "remember me" function, which will ensure that a cookie is stored to identify the user on subsequent site visits.