-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestOneCheckMiddleware.cs
More file actions
39 lines (33 loc) · 1003 Bytes
/
Copy pathRequestOneCheckMiddleware.cs
File metadata and controls
39 lines (33 loc) · 1003 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
namespace APIServer;
public class RequestOneCheckMiddleware
{
private readonly RequestDelegate _next;
RedisLockRepository _lockRepo;
public RequestOneCheckMiddleware(RequestDelegate next, RedisLockRepository lockRepo)
{
_next = next;
_lockRepo = lockRepo;
}
public async Task Invoke(HttpContext context)
{
if (context.Request.Path.Value == "/api/login")
{
await _next(context);
return;
}
string id = context.Request.Headers["UserId"]!;
if (await _lockRepo.LockAsync(id) == false)
{
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Request Only One Time");
return;
}
await _next(context);
if (await _lockRepo.UnLockAsync(id) == false)
{
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Request UnLock Failed");
return;
}
}
}