Lesson 10 of 18 · 7 min read · Updated
Claude Code Permissions for .NET Projects: Allow, Ask and Deny
On this page
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
- Claude Code CLI. This lesson was tested with version
2.1.304. - .NET 10 SDK.
- Source code: Fcakiroglu16/dotnetacademy, branch claude-code-dotnet-permissions
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
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.
- allow: runs without asking. Keep it to commands whose effects you know: building and testing this solution.
- 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.
- 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:
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:
These are the results from the sample. The last column is the tool result text Claude Code returned:
| Request | Rule that matched | Result |
|---|---|---|
dotnet build | allow | Ran, build succeeded |
dotnet test --filter Quote_rejects_zero_weight | allow (--filter *) | Ran: 1 test passed |
dotnet test --no-build | none | This command requires approval |
dotnet publish src/Shipping.Api -c Release -o out | ask | Claude requested permissions to use Bash, but you haven't granted it yet. |
dotnet nuget push missing.nupkg --source ./local-feed | deny | Permission to use Bash with command dotnet nuget push ... has been denied. |
dotnet build && dotnet publish src/Shipping.Api -o out | ask, for the second part | Refused as a whole |
Read .env | deny | File is in a directory that is denied by your permission settings. |
Read src/Shipping.Api/appsettings.Production.json | deny (./**/) | Refused |
Three things stand out:
- Exact rules are exact.
dotnet test --no-buildmatches neitherBash(dotnet test)norBash(dotnet test --filter *). Add families deliberately instead of reaching forBash(dotnet *). - Compound commands are checked part by part. An allowed
dotnet buildcannot carry an unapproveddotnet publishthrough&&. - **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:
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:
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:
- Allowing an interpreter or a script runner is allowing almost everything. Treat
Bash(python3 *),Bash(node *)orBash(dotnet run *)like a broad allow. - 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:
| Effect | Examples | Suggested rule |
|---|---|---|
| Reads the checkout | dotnet --info, dotnet list package | allow |
| Builds and tests this solution | dotnet build, dotnet test | allow, after you checked what the tests touch |
| Changes dependencies | dotnet add package, dotnet tool install | ask |
| Changes a database | dotnet ef database update, a migration bundle | ask, or keep out of the agent's environment |
| Ships something | dotnet publish to a deploy script, dotnet nuget push, git push | ask 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
CLAUDE.mdguides the model..claude/settings.jsonis what Claude Code enforces. The environment decides what is reachable at all.- Project
allowrules only apply after you trust the workspace.denyandaskalways apply. - Rules are evaluated deny, ask, allow. Personal or more specific allows cannot weaken them.
- Exact rules match one command.
*families match more than you think. Compound commands are checked part by part. Readdeny rules also blockcatandgrepon that file, but not an interpreter that builds the path at runtime. Keep secrets out of the checkout.claude -p --setting-sources project,local --output-format jsontests a policy in seconds, and/permissionsshows where each rule comes from.
Source code: github.com/Fcakiroglu16/dotnetacademy/tree/claude-code-dotnet-permissions