# IUserSecretsManager Methods

- Package: [Aspire.Hosting](/reference/api/csharp/aspire.hosting.md)
- Type: [IUserSecretsManager](/reference/api/csharp/aspire.hosting/iusersecretsmanager.md)
- Kind: `Methods`
- Members: `4`

Defines an interface for managing user secrets with support for read and write operations.

## GetOrSetSecret(IConfigurationManager, string, Func<string>)

- Name: `GetOrSetSecret(IConfigurationManager, string, Func<string>)`
- Modifiers: `abstract`

Gets a secret value if it exists in configuration, or sets it using the value generator if it doesn't.

```csharp
public interface IUserSecretsManager
{
    public abstract void GetOrSetSecret(
        IConfigurationManager configuration,
        string name,
        Func<string> valueGenerator)
    {
        // ...
    }
}
```

## Parameters

- `configuration` (`IConfigurationManager`)
  The configuration manager to check and update.
- `name` (`string`)
  The name of the secret.
- `valueGenerator` (`Func<string>`)
  Function to generate the value if it doesn't exist.

## ATS metadata

### Ignored by ATS

- Reason: IConfigurationManager and Func<string> are not ATS-compatible. Use the ATS helper overload that accepts a resource builder and a string value.

## SaveStateAsync(JsonObject, CancellationToken)

- Name: `SaveStateAsync(JsonObject, CancellationToken)`
- Modifiers: `abstract`
- Returns: `Task`

Saves state to user secrets asynchronously (for deployment state manager). If multiple callers save state concurrently, the last write wins.

```csharp
public interface IUserSecretsManager
{
    public abstract Task SaveStateAsync(
        JsonObject state,
        CancellationToken cancellationToken = default(CancellationToken))
    {
        // ...
    }
}
```

## Parameters

- `state` (`JsonObject`)
  The state to save as a JSON object.
- `cancellationToken` (`CancellationToken`) `optional`
  Cancellation token.

## ATS metadata

### Ignored by ATS

- Reason: JsonObject is not ATS-compatible. Use the ATS helper overload that accepts a JSON string.

## TryDeleteSecret(string)

- Name: `TryDeleteSecret(string)`
- Modifiers: `virtual`
- Returns: `bool`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/UserSecrets/IUserSecretsManager.cs#L50)

Attempts to delete a user secret value synchronously.

```csharp
public interface IUserSecretsManager
{
    public virtual bool TryDeleteSecret(
        string name)
    {
        // ...
    }
}
```

## Parameters

- `name` (`string`)
  The name of the secret.

## Returns

`bool` -- True if the secret was deleted successfully; otherwise, false.

## Remarks

The default implementation returns `false` so existing implementations remain compatible.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## TrySetSecret(string, string)

- Name: `TrySetSecret(string, string)`
- Modifiers: `abstract`
- Returns: `bool`

Attempts to set a user secret value synchronously.

```csharp
public interface IUserSecretsManager
{
    public abstract bool TrySetSecret(
        string name,
        string value)
    {
        // ...
    }
}
```

## Parameters

- `name` (`string`)
  The name of the secret.
- `value` (`string`)
  The value of the secret.

## Returns

`bool` -- True if the secret was set successfully; otherwise, false.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.
