ASPX templates
Version 5.x > ASPX templates > Login/Create a user View modes: 
User avatar
Member
Member
hemanthray-gmail - 8/12/2010 2:34:30 PM
   
Login/Create a user
I am trying to create a user in kentico through API, but i dont know how could i add a password to the user. I am using the code below.

UserInfo user = new UserInfo();

user.UserName = "Tessst";

user.FirstName = "Test";

user.LastName = "Testss";

user.FullName = "Sai Rayaprolu";

user.Email = "test@ss.com";

user.IsEditor = true;

user.PreferredCultureCode = "en-us";

user.PasswordFormat = "";


CMS.SiteProvider.UserInfoProvider.SetUserInfo(user);

this code adds the user to kentico but i dont know how can i add a password to that particular user. And is there a way to add that particular user to any roles? and is there any sample where i can login a user programatically

it would be a great help if anyone helps me out in this

Thanks
A

User avatar
Kentico Support
Kentico Support
kentico_radekm - 8/17/2010 1:14:15 AM
   
RE:Login/Create a user
Hello.

You can set user´s password via CMS.SiteProvider.UserInfoProvider.SetPassword() method.

As for "and is there a way to add that particular user to any roles?" - yes, you can see API example here: http://devnet.kentico.com/docs/devguide/managing_user_roles.htm

As for "and is there any sample where i can login a user programatically" - yes, please see example:

// Set authentication cookie

FormsAuthentication.SetAuthCookie(ui.UserName, createPresistentCookie);

// Set context values
SetCurrentUser(new CurrentUserInfo(ui, true));
UserInfoProvider.SetPreferredCultures(ui);


Where ui is appropriate UserInfo object.

Best Regards,
Radek Macalik

User avatar
Member
Member
hemanthray-gmail - 8/24/2010 8:32:36 PM
   
RE:Login/Create a user
Thanks for your reply.

SetCurrentUser function is giving compilation errors. What reference should I add into my project/ what name space should I use.

Thanks

User avatar
Kentico Support
Kentico Support
kentico_radekm - 8/25/2010 12:42:01 PM
   
RE:Login/Create a user
Hello.

Could you please post whole text of this error here? This method is implemented in CMS.CMSHelper.CMSContext class.

Best Regards,
Radek Macalik

User avatar
Member
Member
hemanthray-gmail - 8/25/2010 4:30:33 PM
   
RE:Login/Create a user
Thanks For the Response.


I user this code snippet to log the user in and check in this way but it doesn't log the user into kentico. Please help

1)
userAuth = CMS.SiteProvider.UserInfoProvider.AuthenticateUser(txtUserName.Text, txtPassword.Text, CMS.CMSHelper.CMSContext.CurrentSite.SiteName);
if (userAuth != null)
{
if (Membership.ValidateUser(txtUserName.Text , txtPassword.Text ))
{
FormsAuthentication.SetAuthCookie(userAuth.UserName, false);
CMSContext.SetCurrentUser(new CurrentUserInfo(userAuth, true));
UserInfoProvider.SetPreferredCultures(userAuth);

CMS.GlobalHelper.RequestStockHelper.Remove("CurrentUser");
CMS.GlobalHelper.RequestStockHelper.Add("CurrentUser", new CMS.CMSHelper.CurrentUserInfo(userAuth,true));
}


}


2)
if(CMS.CMSHelper.CMSContext.CurrentUser.IsAuthenticated())
{
//show loggedin Panel

}
else
{
//hide Pannel

}


User avatar
Kentico Support
Kentico Support
kentico_radekm - 10/11/2010 1:22:08 AM
   
RE:Login/Create a user
Hello.

Did you try debug your code to see what line causes problem?

I tried code below and it works:

UserInfo ui = CMS.SiteProvider.UserInfoProvider.AuthenticateUser("Abi", "", CMS.CMSHelper.CMSContext.CurrentSite.SiteName);
if (ui != null)
{
FormsAuthentication.SetAuthCookie(ui.UserName, false);
CMSContext.SetCurrentUser(new CurrentUserInfo(ui, true));
UserInfoProvider.SetPreferredCultures(ui);

CMS.GlobalHelper.RequestStockHelper.Remove("CurrentUser");
CMS.GlobalHelper.RequestStockHelper.Add("CurrentUser", new CMS.CMSHelper.CurrentUserInfo(ui, true));
}


Could you please try it too?

Please note I deleted yours "if (Membership.ValidateUser)" condition, since this method verified that the supplied user name and password are valid, but this is already done by our AuthenticateUser method, which authenticates user against database. So, if this method returns not empty UserInfo object, user exists, therefore user name and password are valid.

Best Regards,
Radek Macalik

User avatar
Member
Member
hemanthray-gmail - 2/22/2011 3:52:39 PM
   
RE:Login/Create a user
thanks for the response radekm, what happens is when i do this
UserInfo ui = CMS.SiteProvider.UserInfoProvider.AuthenticateUser("Abi", "", CMS.CMSHelper.CMSContext.CurrentSite.SiteName);
if (ui != null)
{
FormsAuthentication.SetAuthCookie(ui.UserName, false);
CMSContext.SetCurrentUser(new CurrentUserInfo(ui, true));
UserInfoProvider.SetPreferredCultures(ui);

CMS.GlobalHelper.RequestStockHelper.Remove("CurrentUser");
CMS.GlobalHelper.RequestStockHelper.Add("CurrentUser", new CMS.CMSHelper.CurrentUserInfo(ui, true));
}
chklogin();

then check this

public void chklogin()
{
if (CMS.CMSHelper.CMSContext.CurrentUser.IsAuthenticated())
{

pnlSiteLogin.Visible = true;

}
else
{
//hide Pannel
pnlSiteLogin.Visible = true;

}

}

It say's Im still not authenticated. When i refresh the page it says i am logged in dont know why this happens.

Can you let me know whats the problem

Thanks

User avatar
Certified Developer 8
Certified Developer 8
dvanbale - 2/23/2011 10:04:27 AM
   
RE:Login/Create a user
Hello hemanthray,

Are those methods called in the same postback?

If that's the case, I have experienced the same problem, imagine this scenario.

I place a asp:Login control on my template and create an eventhandler for the Login_LoggedIn event.

I go to my page and log in with a user, the Login_LoggedIn method is hit, indicating a succesfull authentication.

However, when I also execute the CurrentUser.IsAuthenticated() method, inside the Login_LoggedIn method, it returns false!

However, when I refreshed the page and run the IsAuthenticated method again, it returns true.

Kentico is built upon the MembershipProvider architecture, allowing the asp:Login control to work great with it's own UserStore.

When I created a simple ASP.NET Website project with a standard out of the box SQL Membership Provider, with the same asp:Login control, I encountered the same issue!

The HttpContext.Current.Identity.User.Name is empty in the Login_LoggedIn method!

I think this has to do with the HttpContext identity not immediately assuming the logged in identity, unfortenately, I do not know why this is, I still have to research this further.

In my scenario, I just used the values in the username and password textboxes in the asp:Login control to validate a user and redirect him based on the values it contained(what I needed to do at the time)

However, you're problem is somewhat tricky and perhaps Kentico can suggest a better solution, but I would do a redirect to itself (as in the same page) after you've succesfully authenticated your user, its not the best solution, but it should fix your problem in the short term.

Hope this was informative, please let me know if I can help or elaborate any further!


User avatar
Member
Member
hemanthray-gmail - 2/23/2011 11:58:18 AM
   
RE:Login/Create a user
Thanks for the info dvanbale , I did the same for one of our projects but the client did observe that issue and they dont like it.

I used some thing like this

Response.AppendHeader("Refresh", "0;" + Request.RawUrl);

I have a ticket opened with Kentico but i dint get much of a response from them. I will let you know if i find a solution for this. And please pass on if you have one

Thanks for your time

User avatar
Certified Developer 8
Certified Developer 8
dvanbale - 2/23/2011 2:25:14 PM
   
RE:Login/Create a user
Hello Hemanthray,

I did some further research regarding the CurrentUser problem we encountered and instead of typing out a long post, I blogged about it here.

Hope it's informative and helps you with your current issue.

User avatar
Member
Member
hemanthray-gmail - 2/24/2011 9:01:59 AM
   
RE:Login/Create a user
Thanks for sharing it Dave,If can you provide me an example how to update the IPrincipal identity it would help

Thanks

User avatar
Kentico Support
Kentico Support
kentico_radekm - 3/7/2011 5:52:57 AM
   
RE:Login/Create a user
Hello.

IsAuthenticated method calls HttpContext.Current.User.Identity.IsAuthenticated internally (.NET method, not Kentico method) and it is updated in next request. This is why it returns false, although the user is successfully authenticated.

Also in our Logon form web part, LoggedIn event is hit when user has been authenticated only, so it has no sense to call IsAuthenticated() method here again.

You can find more info about this method here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login.loggedin.aspx

Best Regards,
Radek Macalik