Create a user with the .NET SDK
permitClient.Api.CreateUser() creates a user in the environment that your API key belongs to, so you can run permission checks on that user. This reference is for .NET developers who sync users from backend code. To create a user or replace the user if the key already exists, use SyncUser instead.
CreateUser signature
public async Task<UserRead> CreateUser(UserCreate user)
CreateUser parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
user | UserCreate | Yes | The user to create. The UserCreate class is in the PermitSDK.OpenAPI.Models namespace. |
UserCreate properties
| Property | Type | Required | Description |
|---|---|---|---|
Key | string | Yes | A unique ID by which Permit identifies the user. You pass the same key as the user in permit.Check(). Use any value that is unique in your system, such as a user ID from your identity provider. |
Email | string? | No | The email of the user. |
First_name | string? | No | The first name of the user. |
Last_name | string? | No | The last name of the user. |
Attributes | object? | No | User attributes that attribute-based access control (ABAC) policies evaluate, as key-value pairs. |
Role_assignments | ICollection<UserRoleCreate>? | No | Roles that Permit assigns to the user as it creates the user. Each UserRoleCreate has Role (a role key or ID), Tenant (a tenant key or ID), and Resource_instance (a resource instance key in the format resource_type:instance_key, or a resource instance ID). |
Build a UserCreate object named userObj. UserCreate and UserRoleCreate are in the PermitSDK.OpenAPI.Models namespace. Replace the placeholder values with the details of your user:
var userObj = new UserCreate
{
Key = "key",
Email = "email@example.com",
First_name = "John",
Last_name = "Smith",
Attributes = new Dictionary<string, object> { { "department", "Engineering" } },
Role_assignments = new List<UserRoleCreate>
{
new UserRoleCreate { Role = "admin", Tenant = "default" }
}
};
Example: create a user with CreateUser
The example uses a client named permitClient, created with new Permit(...) as shown in Check permissions with the .NET SDK:
var response = await permitClient.Api.CreateUser(userObj);
CreateUser return value and errors
On success, CreateUser returns a UserRead with the user's Key, Id, Email, First_name, Last_name, and Attributes.
If the Permit API returns an error, CreateUser throws a PermitApiException. The exception's StatusCode property has the HTTP status, and its Response property has the response body. The Permit API returns 409 when a user with the same key already exists in the environment.