Cannot access to CMS when logging in from Custom Login Page

Hanh Dang asked on September 17, 2015 06:44

Hi all,

I have a page which requires the users log in to view the content and a Custom Login Webpart with Login event:

private void Login()
{
    if(Membership.Provider.ValidateUser(UserName.Text.Trim(), Password.Text.Trim())
    {
      //Get current user info
      UserInfo ui = MembershipContext.AuthenticatedUser;

      // Set authentication cookie
      FormsAuthentication.SetAuthCookie(ui.UserName, false);

       // Set context values
       CurrentUserInfo cui = new CurrentUserInfo(ui, true);
       CMS.Membership.AuthenticationHelper.SetCurrentUser(cui);
       CultureHelper.SetPreferredUICultureCode(cui.PreferredUICultureCode);

       //Redirect to the page
       if(Condition)
         //Redirect event
       else
         //Other methods
    }
}

After login, I can access to above page, but when i tried to access the CMS system, a error message appeared. The message is:

Access denied to resource 'CMS'
Permission for UI element 'Administration' is required.

The Custom Login Webpart has 2 textboxs to enter Username and Password, a button to call Login event

The user's Privilege level is "Global Administrator". It worked normally when I used Logon Form Webpart (but I don't want to use this Webpart because there are some methods that are required to run after Login event).

Do you have any idea how to solve this?

Thanks,

Hanh Dang

Correct Answer

Brenden Kehren answered on September 17, 2015 20:02

I'm pretty sure you're not logging in the user. The API Examples show this an a way to log a user in:

private bool AuthenticateUser()
{
    // Get the user
    UserInfo user = UserInfoProvider.GetUserInfo("MyNewUser");
    if (user != null)
    {
        if (AuthenticationHelper.AuthenticateUser("MyNewUser", "", SiteContext.CurrentSiteName) != null)
        {
            return true;
        }
    }
    return false;
}

I'd follow that example a bit closer and see what you come up with.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Joshua Adams answered on September 17, 2015 20:25

Also, the logon control ~/CMSWebParts/Membership/Logon/LogonForm.ascx.cs may be a good place to get some of the api usage from. There are a lot of checks that the base logon form does, so I would make sure to include those in the custom control. Did you start by cloning the logon webpart? If not, that may be a good place to start, as it will give you most of the desired functionality, then you can customize that to do whatever before or after you are authenticated. There are three methods that you can control and add to/remove if you need from the cloned logon webpart:

    Login1.LoggedIn += Login1_LoggedIn;
    Login1.LoggingIn += Login1_LoggingIn;
    Login1.Authenticate += Login1_Authenticate;
0 votesVote for this answer Mark as a Correct answer

Hanh Dang answered on September 19, 2015 05:57

@Brenden Kehren: You're right. I have just checked the validation of the user but not logging in. I changed the codes and it's working now.

Replace this

UserInfo ui = MembershipContext.AuthenticatedUser;

by

UserInfo ui = UserInfoProvider.GetUserInfo(UserName.Text.Trim());

@Joshua Adams: Yes, my control is cloned from Login Form Webpart. I want to show a modal instead of redirect/refresh page as the Login Webpart do after logging in. So, I have replace the ASP Login control by my custom login form, but still keeping useful methods.

Thank you!

HanhDang

0 votesVote for this answer Mark as a Correct answer

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