Sync a user with the .NET SDK
permitClient.Api.SyncUser() saves a user's identity and attributes to Permit.io. If no user with the key exists, SyncUser creates the user. If the user exists, SyncUser replaces the stored user with the data you pass. This reference is for .NET developers who sync users from their authentication flow, for example when a user signs up or signs in. After the sync, the policy decision point (PDP) can evaluate permission checks for that user.
SyncUser sends the user with an HTTP PUT request to the Permit API's replace user endpoint. To give the synced user a role, call AssignRole.
SyncUser signature
public async Task<UserRead> SyncUser(UserCreate userCreate)
SyncUser parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
userCreate | UserCreate | Yes | The user to sync. SyncUser reads the user key from userCreate.Key. The UserCreate class is in the PermitSDK.OpenAPI.Models namespace. |
UserCreate properties for SyncUser
SyncUser takes the same UserCreate class as CreateUser:
| 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(). |
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. |
Build a UserCreate object named userObj. 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 = {}
};
Example: sync a user with SyncUser
The example uses a client named permitClient, created with new Permit(...) as shown in Check permissions with the .NET SDK:
var user = await permitClient.Api.SyncUser(userObj);
SyncUser return value and errors
On success, SyncUser returns a UserRead for the created or replaced user.
If the Permit API returns an error, SyncUser throws a PermitApiException. The exception's StatusCode property has the HTTP status, such as 422 when a property fails validation, and its Response property has the response body.