|
| 1 | +# Setting the current user |
| 2 | + |
| 3 | +## .NET Core |
| 4 | + |
| 5 | +To set the current user, you can use the `Zen.SetUser` method in your middleware: |
| 6 | + |
| 7 | +``` csharp |
| 8 | +using Aikido.Zen.DotNetCore; |
| 9 | +using Microsoft.AspNet.Identity; |
| 10 | + |
| 11 | +// ... |
| 12 | + .UseRouting() |
| 13 | + .Use((context, next) => |
| 14 | + { |
| 15 | + // Get the user from your authentication middleware |
| 16 | + var id = context.User?.Identity?.GetUserId(); |
| 17 | + var name = context.User?.Identity?.Name; |
| 18 | + if (!string.IsNullOrEmpty(id)) |
| 19 | + Zen.SetUser(id, name, context); |
| 20 | + return next(); |
| 21 | + }) |
| 22 | + .UseZenFirewall() |
| 23 | +``` |
| 24 | + |
| 25 | +## .NET Framework |
| 26 | + |
| 27 | +In your `Global.asax.cs` file: |
| 28 | + |
| 29 | +``` csharp |
| 30 | +public void Application_Start() |
| 31 | +{ |
| 32 | + // other code |
| 33 | + Zen.SetUser(context => new User(context.User.Identity.GetUserId(), context.User.Identity.Name)); |
| 34 | + Zen.Start(); |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +Or if you are using OWIN, in your `Startup.cs` file: |
| 39 | + |
| 40 | +``` csharp |
| 41 | +using Aikido.Zen.DotNetFramework; |
| 42 | +using Aikido.Zen.Core; |
| 43 | +using Microsoft.AspNet.Identity; |
| 44 | + |
| 45 | +public void Configuration(IAppBuilder app) |
| 46 | +{ |
| 47 | + // other code |
| 48 | + Zen.SetUser(context => new User(context.User.Identity.GetUserId(), context.User.Identity.Name)); |
| 49 | + Zen.Start(); |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +> [!WARNING] |
| 54 | +> Do not call `SetUser` with a shared user ID for unauthenticated or anonymous users (e.g. `SetUser("unauthenticated", "Anonymous")`). When a user is set, rate limiting is applied per user ID instead of per IP address. This means all anonymous users would share a single rate limit bucket and be blocked as a group. For unauthenticated users, simply don't call `SetUser` — rate limiting will automatically fall back to per-IP limiting. |
| 55 | +
|
| 56 | +## Benefits |
| 57 | + |
| 58 | +Using `SetUser` has the following benefits: |
| 59 | + |
| 60 | +- The user ID is used for more accurate rate limiting (you can change IP addresses, but you can't change your user ID). |
| 61 | +- Whenever attacks are detected, the user will be included in the report to Aikido. |
| 62 | +- The dashboard will show all your users, where you can also block them. |
| 63 | +- Passing the user's name is optional, but it can help you identify the user in the dashboard. You will be required to list Aikido Security as a subprocessor if you choose to share personal identifiable information (PII). |
0 commit comments