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
Copy file name to clipboardExpand all lines: README.md
+70Lines changed: 70 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -198,6 +198,76 @@ When using API Key or Basic Authentication, you can specify multiple fixed value
198
198
199
199
With this configuration, authentication will succeed if any of these credentials are provided.
200
200
201
+
**Assigning roles to API Keys and Basic Authentication credentials**
202
+
203
+
You can optionally specify roles for each API Key or Basic Authentication credential. When authentication succeeds, the specified roles will be automatically added as role claims to the user's identity.
204
+
205
+
For single credentials, you can specify roles directly:
206
+
207
+
```json
208
+
"Authentication": {
209
+
"ApiKey": {
210
+
"ApiKeyValue": "f1I7S5GXa4wQDgLQWgz0",
211
+
"UserName": "ApiUser",
212
+
"Roles": ["Administrator"]
213
+
},
214
+
"Basic": {
215
+
"UserName": "marco",
216
+
"Password": "P@$$w0rd",
217
+
"Roles": ["Administrator"]
218
+
}
219
+
}
220
+
```
221
+
222
+
For multiple credentials, you can specify roles for each credential:
223
+
224
+
```json
225
+
"Authentication": {
226
+
"ApiKey": {
227
+
"ApiKeys": [
228
+
{
229
+
"Value": "key-1",
230
+
"UserName": "UserName1",
231
+
"Roles": ["Administrator", "User"]
232
+
},
233
+
{
234
+
"Value": "key-2",
235
+
"UserName": "UserName2",
236
+
"Roles": ["User"]
237
+
}
238
+
]
239
+
},
240
+
"Basic": {
241
+
"Credentials": [
242
+
{
243
+
"UserName": "UserName1",
244
+
"Password": "Password1",
245
+
"Roles": ["Manager", "User"]
246
+
},
247
+
{
248
+
"UserName": "UserName2",
249
+
"Password": "Password2",
250
+
"Roles": ["User"]
251
+
}
252
+
]
253
+
}
254
+
}
255
+
```
256
+
257
+
The `Roles` parameter is optional. If omitted, no role claims will be added to the user's identity. You can then use the standard ASP.NET Core authorization features to check for roles:
**Custom Authentication logic for API Keys and Basic Authentication**
202
272
203
273
If you need to implement custom authentication logic, for example validating credentials with dynamic values and adding claims to identity, you can omit all the credentials in the _appsettings.json_ file and then provide an implementation of [IApiKeyValidator.cs](https://github.com/marcominerva/SimpleAuthentication/blob/master/src/SimpleAuthentication.Abstractions/ApiKey/IApiKeyValidator.cs) or [IBasicAuthenticationValidator.cs](https://github.com/marcominerva/SimpleAuthentication/blob/master/src/SimpleAuthentication.Abstractions/BasicAuthentication/IBasicAuthenticationValidator.cs):
0 commit comments