You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add suffix authentication checks and allowAnonymous option
Introduces a check that throws an InvalidOperationException if a suffix callback is provided and the user is not authenticated, unless allowAnonymous is set to true. Adds the allowAnonymous parameter to all UseDelta overloads, updates documentation and usage examples to explain correct middleware ordering and anonymous scenarios, and adds tests for suffix/authentication behavior.
When using a `suffix` callback that accesses `HttpContext.User` claims, authentication middleware **must** run before `UseDelta`. If `UseDelta` runs first, the User claims won't be populated yet, and all users will get the same cache key.
4
+
5
+
Delta automatically detects this misconfiguration and throws an `InvalidOperationException` with a helpful message if:
6
+
- A `suffix` callback is provided
7
+
- The user is not authenticated (`context.User.Identity?.IsAuthenticated != true`)
8
+
9
+
snippet: SuffixWithAuthEF
10
+
11
+
12
+
### AllowAnonymous
13
+
14
+
For endpoints that intentionally allow anonymous access but still want to use a suffix for cache differentiation (e.g., based on request headers rather than user claims), use `allowAnonymous: true`:
When using a `suffix` callback that accesses `HttpContext.User` claims, authentication middleware **must** run before `UseDelta`. If `UseDelta` runs first, the User claims won't be populated yet, and all users will get the same cache key.
4
+
5
+
Delta automatically detects this misconfiguration and throws an `InvalidOperationException` with a helpful message if:
6
+
- A `suffix` callback is provided
7
+
- The user is not authenticated (`context.User.Identity?.IsAuthenticated != true`)
8
+
9
+
snippet: SuffixWithAuth
10
+
11
+
12
+
### AllowAnonymous
13
+
14
+
For endpoints that intentionally allow anonymous access but still want to use a suffix for cache differentiation (e.g., based on request headers rather than user claims), use `allowAnonymous: true`:
<sup><ahref='/src/Delta.EFTests/Usage.cs#L16-L26'title='Snippet source file'>snippet source</a> | <ahref='#snippet-ShouldExecuteEF'title='Start of snippet'>anchor</a></sup>
154
+
<sup><ahref='/src/Delta.EFTests/Usage.cs#L18-L28'title='Snippet source file'>snippet source</a> | <ahref='#snippet-ShouldExecuteEF'title='Start of snippet'>anchor</a></sup>
155
+
<!-- endSnippet -->
156
+
<!-- endInclude -->
157
+
158
+
159
+
### Suffix and Authentication<!-- include: suffix-auth-ef. path: /docs/mdsource/suffix-auth-ef.include.md -->
160
+
161
+
When using a `suffix` callback that accesses `HttpContext.User` claims, authentication middleware **must** run before `UseDelta`. If `UseDelta` runs first, the User claims won't be populated yet, and all users will get the same cache key.
162
+
163
+
Delta automatically detects this misconfiguration and throws an `InvalidOperationException` with a helpful message if:
164
+
- A `suffix` callback is provided
165
+
- The user is not authenticated (`context.User.Identity?.IsAuthenticated != true`)
166
+
167
+
<!-- snippet: SuffixWithAuthEF -->
168
+
<aid='snippet-SuffixWithAuthEF'></a>
169
+
```cs
170
+
varapp=builder.Build();
171
+
172
+
// Authentication middleware must run before UseDelta
173
+
// so that User claims are available to the suffix callback
174
+
app.UseAuthentication();
175
+
app.UseAuthorization();
176
+
177
+
app.UseDelta<SampleDbContext>(
178
+
suffix: httpContext=>
179
+
{
180
+
// Access user claims to create per-user cache keys
<sup><ahref='/src/Delta.EFTests/Usage.cs#L33-L51'title='Snippet source file'>snippet source</a> | <ahref='#snippet-SuffixWithAuthEF'title='Start of snippet'>anchor</a></sup>
187
+
<!-- endSnippet -->
188
+
189
+
190
+
### AllowAnonymous
191
+
192
+
For endpoints that intentionally allow anonymous access but still want to use a suffix for cache differentiation (e.g., based on request headers rather than user claims), use `allowAnonymous: true`:
193
+
194
+
<!-- snippet: AllowAnonymousEF -->
195
+
<aid='snippet-AllowAnonymousEF'></a>
196
+
```cs
197
+
varapp=builder.Build();
198
+
199
+
// For endpoints that intentionally allow anonymous access
200
+
// but still want a suffix for cache differentiation
Copy file name to clipboardExpand all lines: docs/postgres.md
+55-4Lines changed: 55 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -115,7 +115,58 @@ app.UseDelta(
115
115
returnpath.Contains("match");
116
116
});
117
117
```
118
-
<sup><ahref='/src/DeltaTests/Usage.cs#L19-L29'title='Snippet source file'>snippet source</a> | <ahref='#snippet-ShouldExecute'title='Start of snippet'>anchor</a></sup>
118
+
<sup><ahref='/src/DeltaTests/Usage.cs#L20-L30'title='Snippet source file'>snippet source</a> | <ahref='#snippet-ShouldExecute'title='Start of snippet'>anchor</a></sup>
119
+
<!-- endSnippet -->
120
+
<!-- endInclude -->
121
+
122
+
123
+
### Suffix and Authentication<!-- include: suffix-auth. path: /docs/mdsource/suffix-auth.include.md -->
124
+
125
+
When using a `suffix` callback that accesses `HttpContext.User` claims, authentication middleware **must** run before `UseDelta`. If `UseDelta` runs first, the User claims won't be populated yet, and all users will get the same cache key.
126
+
127
+
Delta automatically detects this misconfiguration and throws an `InvalidOperationException` with a helpful message if:
128
+
- A `suffix` callback is provided
129
+
- The user is not authenticated (`context.User.Identity?.IsAuthenticated != true`)
130
+
131
+
<!-- snippet: SuffixWithAuth -->
132
+
<aid='snippet-SuffixWithAuth'></a>
133
+
```cs
134
+
varapp=builder.Build();
135
+
136
+
// Authentication middleware must run before UseDelta
137
+
// so that User claims are available to the suffix callback
138
+
app.UseAuthentication();
139
+
app.UseAuthorization();
140
+
141
+
app.UseDelta(
142
+
suffix: httpContext=>
143
+
{
144
+
// Access user claims to create per-user cache keys
<sup><ahref='/src/DeltaTests/Usage.cs#L35-L53'title='Snippet source file'>snippet source</a> | <ahref='#snippet-SuffixWithAuth'title='Start of snippet'>anchor</a></sup>
151
+
<!-- endSnippet -->
152
+
153
+
154
+
### AllowAnonymous
155
+
156
+
For endpoints that intentionally allow anonymous access but still want to use a suffix for cache differentiation (e.g., based on request headers rather than user claims), use `allowAnonymous: true`:
157
+
158
+
<!-- snippet: AllowAnonymous -->
159
+
<aid='snippet-AllowAnonymous'></a>
160
+
```cs
161
+
varapp=builder.Build();
162
+
163
+
// For endpoints that intentionally allow anonymous access
164
+
// but still want a suffix for cache differentiation
<sup><ahref='/src/Delta.EFTests/Usage.cs#L16-L26'title='Snippet source file'>snippet source</a> | <ahref='#snippet-ShouldExecuteEF'title='Start of snippet'>anchor</a></sup>
207
+
<sup><ahref='/src/Delta.EFTests/Usage.cs#L18-L28'title='Snippet source file'>snippet source</a> | <ahref='#snippet-ShouldExecuteEF'title='Start of snippet'>anchor</a></sup>
208
+
<!-- endSnippet -->
209
+
<!-- endInclude -->
210
+
211
+
212
+
### Suffix and Authentication<!-- include: suffix-auth-ef. path: /docs/mdsource/suffix-auth-ef.include.md -->
213
+
214
+
When using a `suffix` callback that accesses `HttpContext.User` claims, authentication middleware **must** run before `UseDelta`. If `UseDelta` runs first, the User claims won't be populated yet, and all users will get the same cache key.
215
+
216
+
Delta automatically detects this misconfiguration and throws an `InvalidOperationException` with a helpful message if:
217
+
- A `suffix` callback is provided
218
+
- The user is not authenticated (`context.User.Identity?.IsAuthenticated != true`)
219
+
220
+
<!-- snippet: SuffixWithAuthEF -->
221
+
<aid='snippet-SuffixWithAuthEF'></a>
222
+
```cs
223
+
varapp=builder.Build();
224
+
225
+
// Authentication middleware must run before UseDelta
226
+
// so that User claims are available to the suffix callback
227
+
app.UseAuthentication();
228
+
app.UseAuthorization();
229
+
230
+
app.UseDelta<SampleDbContext>(
231
+
suffix: httpContext=>
232
+
{
233
+
// Access user claims to create per-user cache keys
<sup><ahref='/src/Delta.EFTests/Usage.cs#L33-L51'title='Snippet source file'>snippet source</a> | <ahref='#snippet-SuffixWithAuthEF'title='Start of snippet'>anchor</a></sup>
240
+
<!-- endSnippet -->
241
+
242
+
243
+
### AllowAnonymous
244
+
245
+
For endpoints that intentionally allow anonymous access but still want to use a suffix for cache differentiation (e.g., based on request headers rather than user claims), use `allowAnonymous: true`:
246
+
247
+
<!-- snippet: AllowAnonymousEF -->
248
+
<aid='snippet-AllowAnonymousEF'></a>
249
+
```cs
250
+
varapp=builder.Build();
251
+
252
+
// For endpoints that intentionally allow anonymous access
253
+
// but still want a suffix for cache differentiation
0 commit comments