Claude Code Lesson 10 of 18

Lesson 10 of 18 · 7 min read · Updated

Claude Code Permissions for .NET Projects: Allow, Ask and Deny

Claude Code can read your .NET solution, edit files and run commands. Permission rules decide which of those actions run silently, which need your approval, and which never run. For a .NET repository, "allow everything that starts with dotnet" is not a policy. dotnet build runs MSBuild targets, dotnet test runs arbitrary test code, and dotnet nuget push publishes a package to the world.

In this lesson we write a small permission policy for a .NET 10 solution and test every rule with a real Claude Code session. It is based on Nick Cosentino's article Configure Claude Code Permissions for .NET Development.

Technical Requirements

  1. Claude Code CLI. This lesson was tested with version 2.1.304.
  2. .NET 10 SDK.
  3. Source code: Fcakiroglu16/dotnetacademy, branch claude-code-dotnet-permissions
git clone https://github.com/Fcakiroglu16/dotnetacademy.git
cd dotnetacademy
git switch claude-code-dotnet-permissions
cp .env.example .env

CLAUDE.md Guides, Permissions Enforce

The repository has a CLAUDE.md that says "never publish". That sentence shapes what the model *tries* to do. It does not stop a tool call. Permission rules are checked by Claude Code itself, before a tool runs, whatever the model decides.

There is a third layer: the environment. A process can only use the credentials, files and network it has. Keeping production credentials off a developer machine or CI runner is stronger than any rule. All three layers should say the same thing.

The Policy

Shared rules live in .claude/settings.json and are committed:

.claude/settings.json

{
"$schema": "https://json.schemastore.org/claude-code-settings.json",
"permissions": {
"allow": [
"Bash(dotnet --info)",
"Bash(dotnet build)",
"Bash(dotnet test)",
"Bash(dotnet test --filter *)"
],
"ask": [
"Bash(dotnet add *)",
"Bash(dotnet publish *)",
"Bash(dotnet ef database update *)"
],
"deny": [
"Read(./.env)",
"Read(./**/appsettings.Production.json)",
"Bash(dotnet nuget push *)"
]
}
}

A rule is a tool name with an optional specifier. Bash(dotnet build) matches exactly that command. Bash(dotnet test --filter *) matches the family of commands that start with dotnet test --filter . Read(./**/appsettings.Production.json) uses gitignore-style paths relative to the project.

  1. allow: runs without asking. Keep it to commands whose effects you know: building and testing this solution.
  2. ask: always prompts, even if another settings file allows the command. Use it for actions with side effects outside the checkout: adding packages, publishing, applying migrations.
  3. deny: never runs. Nothing can override it.

Trust Comes First

Our first test failed in a surprising way. dotnet build is in the allow list, but a headless session refused it. The debug log explained why:

Ignoring 4 permissions.allow entries from .claude/settings.json: this workspace has not been trusted.

A freshly cloned repository could allow anything for itself, so Claude Code ignores its allow rules until you accept the workspace trust dialog. Start claude in the folder once and accept it. deny and ask rules apply even before trust, because they only make the session stricter. In the untrusted folder, dotnet nuget push was still denied and dotnet publish still asked, while dotnet build fell back to This command requires approval.

Testing the Rules Headlessly

You can check a rule without clicking through prompts. In print mode (-p) nobody can answer a prompt, so anything that would ask is refused and listed in the JSON result. --setting-sources project,local leaves your user settings out, so only the repository's rules apply:

claude -p 'Run the bash command `dotnet publish src/Shipping.Api -c Release -o out`' \
--setting-sources project,local --output-format json

These are the results from the sample. The last column is the tool result text Claude Code returned:

RequestRule that matchedResult
dotnet buildallowRan, build succeeded
dotnet test --filter Quote_rejects_zero_weightallow (--filter *)Ran: 1 test passed
dotnet test --no-buildnoneThis command requires approval
dotnet publish src/Shipping.Api -c Release -o outaskClaude requested permissions to use Bash, but you haven't granted it yet.
dotnet nuget push missing.nupkg --source ./local-feeddenyPermission to use Bash with command dotnet nuget push ... has been denied.
dotnet build && dotnet publish src/Shipping.Api -o outask, for the second partRefused as a whole
Read .envdenyFile is in a directory that is denied by your permission settings.
Read src/Shipping.Api/appsettings.Production.jsondeny (./**/)Refused

Three things stand out:

  1. Exact rules are exact. dotnet test --no-build matches neither Bash(dotnet test) nor Bash(dotnet test --filter *). Add families deliberately instead of reaching for Bash(dotnet *).
  2. Compound commands are checked part by part. An allowed dotnet build cannot carry an unapproved dotnet publish through &&.
  3. **ask and deny feel the same in -p.** Both are refused. In an interactive session, ask shows a prompt, and deny never does.

Personal Settings Cannot Weaken the Project

.claude/settings.local.json is for your own rules and is git-ignored. To test the rule order, we allowed everything the project gates:

{
"permissions": {
"allow": [
"Bash(dotnet publish *)",
"Bash(dotnet nuget push *)",
"Bash(cat *)"
]
}
}

Nothing changed. dotnet publish still asked and dotnet nuget push was still denied. Rules are evaluated in the order deny, ask, allow, and the first match wins no matter which file it came from. A more specific allow never beats a broader ask or deny. When a rule you added seems to do nothing, run /permissions in a session. It lists every rule and the file it came from.

Settings files are merged from managed policy (highest), command-line arguments, .claude/settings.local.json, .claude/settings.json and ~/.claude/settings.json (lowest). Organizations can lock rules in managed settings that no project can change.

Where Deny Rules Stop

With Bash(cat *) allowed, cat .env was still refused: Permission to use Bash with command cat .env has been denied. grep KEY .env was refused too. Claude Code applies Read deny rules to shell commands that clearly name the file.

Then we allowed Bash(python3 *) and asked for a command that builds the path at runtime:

python3 -c "print(open('.' + '/.env').read())"
CARRIER_API_KEY=fake-local-key-for-the-lesson

The file was read. A rule matches the text of a command. It cannot follow what an interpreter does at runtime. The same is true for dotnet run, a test that reads files, or any script you allow. Two lessons follow:

  1. Allowing an interpreter or a script runner is allowing almost everything. Treat Bash(python3 *), Bash(node *) or Bash(dotnet run *) like a broad allow.
  2. Real secrets should not be in the working directory at all. Use user secrets, environment variables injected at run time, or a secret store, and keep production credentials off the machine where the agent runs.

Permission Modes

The mode changes how unmatched actions are handled. claude --help lists manual (the default: prompt), acceptEdits (file edits in the working directory are accepted without asking, shell commands are not), plan (explore and plan without editing), dontAsk (refuse anything that would prompt), auto (a classifier decides) and bypassPermissions (skip permission prompts). Switch with Shift+Tab or --permission-mode. No mode makes a command safe. It only decides who approves it.

.NET Commands by Effect

Group commands by what they touch, not by their first word:

EffectExamplesSuggested rule
Reads the checkoutdotnet --info, dotnet list packageallow
Builds and tests this solutiondotnet build, dotnet testallow, after you checked what the tests touch
Changes dependenciesdotnet add package, dotnet tool installask
Changes a databasedotnet ef database update, a migration bundleask, or keep out of the agent's environment
Ships somethingdotnet publish to a deploy script, dotnet nuget push, git pushask or deny

Generating an EF Core migration only edits source files. Applying it changes a database. Approving the first is not approval for the second.

Summary

  1. CLAUDE.md guides the model. .claude/settings.json is what Claude Code enforces. The environment decides what is reachable at all.
  2. Project allow rules only apply after you trust the workspace. deny and ask always apply.
  3. Rules are evaluated deny, ask, allow. Personal or more specific allows cannot weaken them.
  4. Exact rules match one command. * families match more than you think. Compound commands are checked part by part.
  5. Read deny rules also block cat and grep on that file, but not an interpreter that builds the path at runtime. Keep secrets out of the checkout.
  6. claude -p --setting-sources project,local --output-format json tests a policy in seconds, and /permissions shows where each rule comes from.

Source code: github.com/Fcakiroglu16/dotnetacademy/tree/claude-code-dotnet-permissions