Portal Engine
Version 3.x > Portal Engine > How to programmatically log in a user View modes: 
User avatar
Member
Member
jdavies-uniteform - 8/25/2008 1:01:38 PM
   
How to programmatically log in a user
Hi,

Can anyone tell me how I can programmatically log a user in (without them having to complete the login form)?
I'll know the CMS_User.UserID of the user, it's a secure channel so there are no security concerns - but I need to log them in without them completing the form. Is there a function I can call, or do I need to set a session/cookie value?

thanks,

Jez

User avatar
Member
Member
jdavies-uniteform - 8/26/2008 6:56:24 AM
   
RE:How to programmatically log in a user
can anyone help me please?

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 8/28/2008 5:54:46 AM
   
RE:How to programmatically log in a user
Hi,

I know that you have received a reply from my colleague via e-mail. I hope it helped. I am posting here his answer also here for the case if someone will need it also.

Here is the answer:

CMS.SiteProvider.UserInfo uI = CMS.SiteProvider.UserInfoProvider.AuthenticateUser("USERNAME", "PASSWORD", CMS.CMSHelper.CMSContext.CurrentSite.SiteName);

if (uI != null)
{
if (Membership.ValidateUser("USERNAME", "PASSWORD"))
{
FormsAuthentication.SetAuthCookie("USERNAME", false);
CMS.GlobalHelper.RequestStockHelper.Remove("CurrentUser");
CMS.GlobalHelper.RequestStockHelper.Add("CurrentUser", new CMS.CMSHelper.CurrentUserInfo(uI));
}
}

If you know user's userID, you can create your own query to get user name and password from database and use them in AuthenticateUser method.
You can see our API documentation for more information about methods I mentioned in the example:
http://devnet.kentico.com/Documentation.aspx


Best Regards,
Juraj Ondrus