PromptPlus

PromptPlus # PromptPlus ## **Secret** [![NuGet](https://img.shields.io/badge/NuGet-PromptPlus-blue)](https://www.nuget.org/packages/PromptPlus) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![.NET](https://img.shields.io/badge/.NET-8%20%7C%209%20%7C%2010-512BD4)](https://dotnet.microsoft.com/)

← Back to HomeNext: Secret — Methods →


Single-line, masked text entry. The user types, each character is hidden behind a mask symbol, and they confirm with Enter.

The Secret control is the masked sibling of Input. Use it whenever the value must not appear on screen — a password, an API key, a PIN, a connection string, a token. It shares the same live filtering, case coercion, length cap, and confirmation-time validation as Input, but replaces every typed character with a mask symbol and can optionally let the user peek at the plain text with F2.

⚠️ Secret deliberately omits the persistence features of Input (no history, no autocomplete, no seeded default). A secret should never be written to disk or offered as a suggestion — see the security note in Operations.


On this page

Sub-page What you will find
Index (this page) What it is, when to use it, a first working example, the method map
Methods Every fluent method — signature, parameters, defaults, and a snippet
Operations Keyboard, the mask character, F2 reveal, validation flow, and the security note
Styles The InputStyles regions and how to recolor them

When to use it

Use Secret when… Consider instead…
The value is a password, key, PIN, or token
The value is ordinary free-form text Input
The value must match a fixed pattern (date, phone, currency) MaskEdit
The user should pick from a known list Select

Minimal example

using PromptPlusLibrary;

var result = PromptPlus.Controls
    .Secret("Password")
    .Run();

if (!result.IsAborted)
    PromptPlus.Console.WriteLine("Password captured.");

⚠️ Always check IsAborted before using .Content. On abort, .Content is NOT guaranteed to be empty — it holds whatever partial text was typed before Esc was pressed. Treat it as sensitive and discard it; don’t assume it’s safe to leave lying around just because the user aborted. Never echo .Content to the console — the example above prints a confirmation, not the value.


A more complete example

using PromptPlusLibrary;
using ConsolePlusLibrary;   // Color, InputStyles-agnostic types

var pin = PromptPlus.Controls
    .Secret("PIN", "Only 4 digits are accepted")
    .MaskSecret('*', enabledView: false)   // hide with '*', no F2 reveal
    .AcceptInput(char.IsDigit)             // reject any non-digit keystroke
    .MaxLength(4)                          // stop accepting after 4 characters
    .PredicateValid(v => v.Length == 4
        ? (true, null)
        : (false, "PIN must be exactly 4 digits"))   // validate on Enter
    .Run();

if (!pin.IsAborted)
    PromptPlus.Console.WriteLine("PIN accepted.");

This combines the four most common building blocks: a custom mask with reveal disabled (MaskSecret), per-keystroke filtering (AcceptInput), a hard length cap (MaxLength), and confirmation-time validation with a message (PredicateValid). See Operations for how they interact.


Method map

Grouped by purpose. Full signatures and examples are on the Methods page.

Purpose Methods
Masking & reveal MaskSecret
Restrict typing AcceptInput, MaxLength, InputToCase
Validate on confirm PredicateValid, PredicateValidAsync
Dynamic description ChangeDescription, ChangeDescriptionAsync
Appearance & behavior Styles, Options
Run Run

Unlike Input, Secret has no Default, DefaultIfEmpty, EnableHistory, or suggestion methods — by design.


Return value

Secret returns ResultPrompt<string>.

Member Meaning
.Content The confirmed text — or whatever partial text was typed before an abort, not necessarily empty
.IsAborted true when the user pressed Esc / the abort key
var (secret, aborted) = PromptPlus.Controls.Secret("Token").Run();
if (!aborted) UseToken(secret);   // consume it — do not print it

See also