---
title: Users
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

List of examples:

## Dependency injection

### Initialize required services

```csharp
// Initializes all services and provider classes used within
// the API examples on this page using dependency injection.
private readonly IUserInfoProvider userInfoProvider;

public UsersServices(IUserInfoProvider userInfoProvider)
{
    this.userInfoProvider = userInfoProvider;
}
```

[> Back to list of examples](#toc)

## Users

### Create new users with administration access

```csharp
// Creates a new user object
UserInfo newUser = new UserInfo();

// Sets the user properties
newUser.UserName = "NewUser";
newUser.Email = "new.user@domain.com";

// Enables the user
newUser.UserEnabled = true;

// Allows the user to access the administration interface
// Also controls whether the user is displayed in the Users application in the administration
newUser.UserAdministrationAccess = true;

// Saves the user to the database
userInfoProvider.Set(newUser);
```

[> Back to list of examples](#toc)

### Update existing users

```csharp
// Gets the user
UserInfo updateUser = userInfoProvider.Get("NewUser");
if (updateUser != null)
{
    // Updates the user's properties
    updateUser.FirstName = updateUser.FirstName.ToLowerInvariant();
    updateUser.LastName = updateUser.LastName.ToLowerInvariant();

    // Saves the changes to the database
    userInfoProvider.Set(updateUser);
}
```

[> Back to list of examples](#toc)

### Update multiple users

```csharp
// Gets all users whose username starts with 'NewUser'
var users = userInfoProvider.Get().WhereStartsWith("UserName", "NewUser");

// Loops through individual users
foreach (UserInfo modifyUser in users)
{
    // Updates the user properties
    modifyUser.FirstName = modifyUser.FirstName.ToUpper();
    modifyUser.LastName = modifyUser.LastName.ToUpper();

    // Saves the changes to the database
    userInfoProvider.Set(modifyUser);
}
```

[> Back to list of examples](#toc)

### Work with custom user fields

```csharp
// Gets the user
UserInfo user = userInfoProvider.Get("NewUser");

if (user != null)
{
    // Attempts to retrieve a value from a custom text field named 'CustomField'
    string value = user.GetValue("CustomField", "");

    // Sets a modified value for the custom field
    user.SetValue("CustomField", value + "_customSuffix");

    // Saves the changes to the database
    userInfoProvider.Set(user);
}
```

[> Back to list of examples](#toc)

### Delete users

```csharp
// Gets the user
UserInfo deleteUser = userInfoProvider.Get("NewUser");

if (deleteUser != null)
{
    // Deletes the user
    userInfoProvider.Delete(deleteUser);
}
```

[> Back to list of examples](#toc)

## User authentication and authorization

### Check user administration access

```csharp
// Gets the user
UserInfo user = userInfoProvider.Get("NewUser");

if (user != null)
{
    // Checks if the user has access to the admin UI
    if (user.HasAdministrationAccess())
    {
        // Perform an action
    }
}
```

[> Back to list of examples](#toc)
