-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenCheckMiddleware.cs
More file actions
48 lines (42 loc) · 1.35 KB
/
Copy pathTokenCheckMiddleware.cs
File metadata and controls
48 lines (42 loc) · 1.35 KB
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
40
41
42
43
44
45
46
47
48
namespace APIServer;
public class TokenCheckMiddleware
{
private readonly RequestDelegate _next;
IMemoryRepository _memoryRepo;
public TokenCheckMiddleware(RequestDelegate next, IMemoryRepository memoryRepo)
{
_next = next;
_memoryRepo = memoryRepo;
}
public async Task Invoke(HttpContext context)
{
if (context.Request.Path.Value == "/api/login")
{
await _next(context);
return;
}
else if (!context.Request.Headers.ContainsKey("Authorization") ||
!context.Request.Headers.ContainsKey("UserId"))
{
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Token And UserId Not Found");
return;
}
string token = context.Request.Headers["Authorization"]!;
string id = context.Request.Headers["UserId"]!;
string? accessToken = await _memoryRepo.GetAccessToken(id);
if (id == null)
{
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Token Not Found");
return;
}
if(token != accessToken)
{
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Token Not Match");
return;
}
await _next(context);
}
}