Assign a role to a user with the .NET SDK
permitClient.Api.AssignRole() assigns a role to a user within one tenant. The role applies only in that tenant. This reference is for .NET developers who grant permissions from backend code.
AssignRole signature
public async Task<RoleAssignmentRead> AssignRole(string userKey, string roleKey, string tenantKey, string resourceInstanceId = null, string resource_type = null)
AssignRole parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
userKey | string | Yes | The key of the user who gets the role. |
roleKey | string | Yes | The key of the role to assign. |
tenantKey | string | Yes | The key of the tenant in which the role applies. |
resourceInstanceId | string | No | The key of a resource instance, such as doc1, to assign a resource role on that instance only. Pass it together with resource_type. |
resource_type | string | No | The key of the resource type of the instance, such as document. Pass it together with resourceInstanceId. |
If you pass only one of resourceInstanceId and resource_type, AssignRole throws an ArgumentException before it calls the Permit API. When you pass both, the SDK sends the resource instance as <resource_type>:<resourceInstanceId>, for example document:doc1.
Example: assign a role with AssignRole
The example uses a client named permitClient, created with new Permit(...) as shown in Check permissions with the .NET SDK. userId, roleId, and tenantId hold the keys of the user, the role, and the tenant:
var response = await permitClient.Api.AssignRole(userId, roleId, tenantId);
AssignRole return value and errors
On success, AssignRole returns a RoleAssignmentRead with the User, Role, Tenant, and Resource_instance of the new role assignment.
If the Permit API returns an error, AssignRole throws a PermitApiException. The exception's StatusCode property has the HTTP status, such as 422 when an argument fails validation, and its Response property has the response body.