> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ofauth.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C# SDK

> Read a profile, connect users, and store OAuth tokens safely.

Use .NET 8 or later on your backend (your app's server). Get an access token with the `profile:read` permission and a linked account ID from [Connect an account](https://docs.ofauth.com/v3/guides/link.md).

## Install

Use the [HTTP quickstart](https://docs.ofauth.com/v3/setup/quickstart.md) until the SDK release candidate is published.

In your .NET project, run:

```bash theme={null}
dotnet add package OnlyFans.SDK --version 4.0.0-rc.1
```

## Read the current profile

Set `OFAUTH_ACCESS_TOKEN` and `OFAUTH_LINKED_ACCOUNT_ID` in your server's environment. In a console app, use:

```csharp theme={null}
using OFAuth;

var accessToken = Environment.GetEnvironmentVariable("OFAUTH_ACCESS_TOKEN")
	?? throw new InvalidOperationException("Set OFAUTH_ACCESS_TOKEN");
var accountId = Environment.GetEnvironmentVariable("OFAUTH_LINKED_ACCOUNT_ID")
	?? throw new InvalidOperationException("Set OFAUTH_LINKED_ACCOUNT_ID");

var client = new OFAuthClient(accessToken, accountId);
var profile = await client.Access.Profile.GetAsync();
Console.WriteLine($"{profile.Id} {profile.Username} {profile.Name}");
```

The SDK sends the access token and linked account header for you.

## Check the result

The program prints the account ID, username, and name. Missing values stay `null` and appear as empty text in this output. This request does not change the account.

If the API returns an error, the SDK throws `OFAuthApiException`. Read its `Code`, `RecoveryAction`, and `RequestId`. Follow [Handle failed requests](https://docs.ofauth.com/v3/reference/error-handling.md) to choose the next step. Check whether a message or post was created before repeating a request whose result you did not receive.

## Use the client in your application

Use `IterateAsync()` to read all pages of a list. For example, list the accounts shared through this authorization:

```csharp theme={null}
await foreach (var account in client.LinkedAccounts.IterateAsync())
{
	Console.WriteLine($"{account.Id} {account.DisplayName}");
}
```

Use `client.WithLinkedAccount(accountId)` to select another account shared through the same authorization. Pass a `CancellationToken` to requests when your app needs to stop them.

## Connect customers and renew access

Use the SDK's OAuth helper or a standard confidential OAuth client with the [authorization-code contract](https://docs.ofauth.com/v3/api-reference/link/authorize.md). Persist callback state and PKCE against the initiating application user and browser session, with expiry and single use. Validate the issuer and exact redirect URI before exchange.

Your auth framework or application owns token persistence and refresh. Supply its current access token to the resource client. Use the returned `expires_in`, and refresh through the form-encoded [token endpoint](https://docs.ofauth.com/v3/api-reference/link/tokens.md). Code exchanges always return a refresh token.

The refresh token is stable and bound to the authenticated client. Independent instances can refresh concurrently; retry a lost refresh response with the same token. No database refresh lock, attempt marker, or replay journal is required. Use ordinary conditional writes so an earlier refresh cannot replace a registration the customer has changed or disconnected.

Keep client and environment in each application's customer storage key. Discover all granted accounts, then persist an explicit account choice. An account ID does not choose authority or billing.

Revoking a refresh token ends that credential and its access tokens. Ending the authorization ends all its credentials. A provider session error requires [account reconnect](https://docs.ofauth.com/v3/guides/reconnect.md), which does not renew OAuth or replay a write. See the [Express source example](https://github.com/ofauth-source/example-express-oauth) for the complete backend journey.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.