Kentico CMS 7.0 Developer's Guide

Managing users

Managing users

Previous topic Next topic Mail us feedback on this topic!  

Managing users

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example creates a user.

 

private void CreateUser()
{
    // Create new user object
    UserInfo newUser = new UserInfo();
 
    // Set the properties
    newUser.FullName = "My new user";
    newUser.UserName = "MyNewUser";
 
    // Save the user
    UserInfoProvider.SetUserInfo(newUser);
}

 

The following example gets and updates a user.

 

private bool GetAndUpdateUser()
{
    // Get the user
    UserInfo updateUser = UserInfoProvider.GetUserInfo("MyNewUser");
    if (updateUser != null)
    {
        // Update the properties
        updateUser.FullName = updateUser.FullName.ToLower();
 
        // Save the changes
        UserInfoProvider.SetUserInfo(updateUser);
 
        return true;
    }
 
    return false;
}

 

The following example gets and bulk updates users.

 

private bool GetAndBulkUpdateUsers()
{
    // Prepare the parameters
    string where = "UserName LIKE N'MyNewUser%'";
 
    // Get the data
    DataSet users = UserInfoProvider.GetUsers(where, null);
    if (!DataHelper.DataSourceIsEmpty(users))
    {
        // Loop through the individual items
        foreach (DataRow userDr in users.Tables[0].Rows)
        {
            // Create object from DataRow
            UserInfo modifyUser = new UserInfo(userDr);
 
            // Update the properties
            modifyUser.FullName = modifyUser.FullName.ToUpper();
 
            // Save the changes
            UserInfoProvider.SetUserInfo(modifyUser);
        }
 
        return true;
    }
 
    return false;
}

 

The following example deletes a user.

 

private bool DeleteUser()
{
    // Get the user
    UserInfo deleteUser = UserInfoProvider.GetUserInfo("MyNewUser");
 
    // Delete the user
    UserInfoProvider.DeleteUser(deleteUser);
 
    return (deleteUser != null);
}

 

The following example checks if the credentials of a user are valid for a specified site using forms authentication.

 

private bool AuthenticateUser()
{
    // Get the user
    UserInfo user = UserInfoProvider.GetUserInfo("MyNewUser");
    if (user != null)
    {

      if (AuthenticationHelper.AuthenticateUser("MyNewUser", "", CMSContext.CurrentSiteName) != null)

        {
            return true;
        }
    }
 
    return false;
}