Search code examples
c#asp.netkentico

Log a user in to my front end website using kentico


What is a way that I can log a user in to secure front end area in Kentico? Should I use the ASP.NET membership API?


Solution

  • assuming that you have 2 textboxes and an event handler (txtEmail, txtPassword and btnSubmit_Click ) and that you have setup the appropriate roles in the kentico site manager you can use something like this:

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            CMS.SiteProvider.UserInfo ui = CMS.SiteProvider.UserInfoProvider.AuthenticateUser(txtEmail.Text, txtPassword.Text, CMS.CMSHelper.CMSContext.CurrentSite.SiteName);
            if (ui != null)
            {
                System.Web.Security.FormsAuthentication.SetAuthCookie(ui.UserName, true);
                CMS.CMSHelper.CMSContext.SetCurrentUser(new CMS.CMSHelper.CurrentUserInfo(ui, true));
                CMS.SiteProvider.UserInfoProvider.SetPreferredCultures(ui);
                Response.Redirect("MY_SECURE_PAGE");
            }
            else
            {
                litMessage.Text = "Email/Password incorrect";
            }
        }