Revize 3a6c189b
Přidáno uživatelem Lukáš Vlček před asi 3 roky(ů)
Backend/Backend/Controllers/AuthController.cs | ||
---|---|---|
6 | 6 |
using Microsoft.AspNetCore.Mvc; |
7 | 7 |
using Models.Authentication; |
8 | 8 |
using RestAPI.Controllers.Common; |
9 |
using RestAPI.Utils; |
|
9 | 10 |
|
10 | 11 |
namespace RestAPI.Controllers; |
11 | 12 |
|
... | ... | |
48 | 49 |
} |
49 | 50 |
|
50 | 51 |
[HttpGet("/auth/test")] |
51 |
[ProducesResponseType((int) HttpStatusCode.OK, Type = typeof(User))]
|
|
52 |
[ProducesResponseType((int) HttpStatusCode.OK, Type = typeof(ClientInfo))]
|
|
52 | 53 |
[ProducesResponseType((int) HttpStatusCode.Forbidden)] |
53 |
public ActionResult<User> TestLogged([FromServices] User loggedUser)
|
|
54 |
public ActionResult<ClientInfo> TestLogged([FromServices] ClientInfo clientInfo)
|
|
54 | 55 |
{ |
55 |
return loggedUser; |
|
56 |
return clientInfo; |
|
57 |
} |
|
58 |
|
|
59 |
|
|
60 |
[AllowAnonymous] |
|
61 |
[HttpGet("/auth/test/aa")] |
|
62 |
[ProducesResponseType((int) HttpStatusCode.OK, Type = typeof(ClientInfo))] |
|
63 |
[ProducesResponseType((int) HttpStatusCode.Forbidden)] |
|
64 |
public ActionResult<ClientInfo> TestLoggedAA([FromServices] ClientInfo clientInfo) |
|
65 |
{ |
|
66 |
return clientInfo; |
|
56 | 67 |
} |
57 | 68 |
} |
Backend/Backend/Program.cs | ||
---|---|---|
8 | 8 |
using Microsoft.EntityFrameworkCore; |
9 | 9 |
using Microsoft.OpenApi.Models; |
10 | 10 |
using RestAPI.Middleware; |
11 |
using RestAPI.Utils; |
|
11 | 12 |
using Serilog; |
12 | 13 |
|
13 | 14 |
var builder = WebApplication.CreateBuilder(args); |
... | ... | |
29 | 30 |
builder.Services.AddSwaggerGen(c => |
30 | 31 |
{ |
31 | 32 |
c.SwaggerDoc("v1", |
32 |
new OpenApiInfo { Title = "AnnotationTool", Description = "KIV/ASWI ZČU Plzeň, 2022", Version = "0.1.1" });
|
|
33 |
new OpenApiInfo {Title = "AnnotationTool", Description = "KIV/ASWI ZČU Plzeň, 2022", Version = "0.1.1"});
|
|
33 | 34 |
}); |
34 | 35 |
|
35 | 36 |
// JWT authentication |
... | ... | |
50 | 51 |
); |
51 | 52 |
|
52 | 53 |
builder.Services.AddHttpContextAccessor(); |
53 |
builder.Services.AddScoped<User>(provider =>
|
|
54 |
((User?)provider.GetRequiredService<IHttpContextAccessor>().HttpContext?.Items["User"]));
|
|
54 |
builder.Services.AddScoped<ClientInfo>(provider =>
|
|
55 |
ContextUtils.GetClientInfo(provider.GetRequiredService<IHttpContextAccessor>().HttpContext));
|
|
55 | 56 |
|
56 | 57 |
// Database |
57 | 58 |
builder.Services.AddDbContext<DatabaseContext>(); |
Backend/Backend/Utils/ClientInfo.cs | ||
---|---|---|
1 |
using Core.Entities; |
|
2 |
|
|
3 |
namespace RestAPI.Utils; |
|
4 |
|
|
5 |
public class ClientInfo |
|
6 |
{ |
|
7 |
public bool IsLogged => LoggedUser != null; |
|
8 |
|
|
9 |
public User? LoggedUser { get; set; } = null; |
|
10 |
|
|
11 |
public string IP { get; set; } = "unknown"; |
|
12 |
} |
Backend/Backend/Utils/ContextUtils.cs | ||
---|---|---|
1 |
using Core.Entities; |
|
2 |
using Serilog; |
|
3 |
|
|
4 |
namespace RestAPI.Utils; |
|
5 |
|
|
6 |
public static class ContextUtils |
|
7 |
{ |
|
8 |
private static string GetIP(HttpContext httpContext) |
|
9 |
{ |
|
10 |
try |
|
11 |
{ |
|
12 |
return httpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown"; |
|
13 |
} |
|
14 |
catch (Exception ex) |
|
15 |
{ |
|
16 |
Log.Logger.Warning("Not able to GetIP(): {Exception}", ex); |
|
17 |
return "unknown"; |
|
18 |
} |
|
19 |
} |
|
20 |
|
|
21 |
public static ClientInfo GetClientInfo(HttpContext? context) |
|
22 |
{ |
|
23 |
var ci = new ClientInfo(); |
|
24 |
if (context == null) |
|
25 |
{ |
|
26 |
return ci; |
|
27 |
} |
|
28 |
|
|
29 |
string IP = GetIP(context); |
|
30 |
ci.IP = IP; |
|
31 |
|
|
32 |
User? loggedUser = (User?) context.Items["User"]; |
|
33 |
if (loggedUser == null) |
|
34 |
{ |
|
35 |
ci.LoggedUser = null; |
|
36 |
} |
|
37 |
else |
|
38 |
{ |
|
39 |
ci.LoggedUser = loggedUser; |
|
40 |
} |
|
41 |
|
|
42 |
return ci; |
|
43 |
} |
|
44 |
} |
Také k dispozici: Unified diff
ClientInfo in DependencyInjection
ClientInfo contains IP and info about logged user