Добавлено управление сессиями
This commit is contained in:
47
PlaylistShared.Api/Services/UserSessionService.cs
Normal file
47
PlaylistShared.Api/Services/UserSessionService.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using PlaylistShared.Api.Data;
|
||||
using PlaylistShared.Api.Entities;
|
||||
|
||||
public class UserSessionService
|
||||
{
|
||||
private readonly ApplicationDbContext _db;
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
public UserSessionService(ApplicationDbContext db, IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_db = db;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
public async Task<UserSession> GetOrCreateCurrentSessionAsync(Guid? associatedUserId = null)
|
||||
{
|
||||
var httpContext = _httpContextAccessor.HttpContext
|
||||
?? throw new InvalidOperationException("No HttpContext available");
|
||||
|
||||
var sessionId = httpContext.Session.Id;
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
var session = await _db.UserSessions.FindAsync(sessionId);
|
||||
if (session == null)
|
||||
{
|
||||
session = new UserSession
|
||||
{
|
||||
SessionId = sessionId,
|
||||
ClientIpAddress = httpContext.Connection.RemoteIpAddress?.ToString(),
|
||||
UserAgent = httpContext.Request.Headers["User-Agent"].ToString(),
|
||||
FirstSeenUtc = now,
|
||||
LastSeenUtc = now,
|
||||
AssociatedUserId = associatedUserId
|
||||
};
|
||||
_db.UserSessions.Add(session);
|
||||
}
|
||||
else
|
||||
{
|
||||
session.LastSeenUtc = now;
|
||||
if (session.AssociatedUserId == null && associatedUserId != null)
|
||||
session.AssociatedUserId = associatedUserId;
|
||||
_db.UserSessions.Update(session);
|
||||
}
|
||||
await _db.SaveChangesAsync();
|
||||
return session;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user