From 9b4453f4b14ae2b70c39e4ac225eb294f5b019c5 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sun, 18 Oct 2026 05:46:49 +0330 Subject: [PATCH] last ... --- Interfaces/IXIdentityManager.cs | 5 ++ .../XIdentityManager/XIdentityManager+Open.cs | 50 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/Interfaces/IXIdentityManager.cs b/Interfaces/IXIdentityManager.cs index 69286f9..9012331 100644 --- a/Interfaces/IXIdentityManager.cs +++ b/Interfaces/IXIdentityManager.cs @@ -468,6 +468,11 @@ namespace xIds.Interfaces CancellationToken cancellationToken = default ); + Task UseApiKey( + XOpenActionRequestDto dto, + CancellationToken cancellationToken = default + ); + Task GetUserInfo( XOpenActionRequestDto dto ); diff --git a/Providers/XIdentityManager/XIdentityManager+Open.cs b/Providers/XIdentityManager/XIdentityManager+Open.cs index 9acb7c7..1fb0e87 100644 --- a/Providers/XIdentityManager/XIdentityManager+Open.cs +++ b/Providers/XIdentityManager/XIdentityManager+Open.cs @@ -7,6 +7,7 @@ using xExceptions.Constants; using System.Threading.Tasks; using System.Collections.Generic; using Microsoft.EntityFrameworkCore; +using xIdentityModels.Models; namespace xIds.Providers { @@ -166,6 +167,55 @@ namespace xIds.Providers return result; } + /// + /// Use Specified Api Key ... + /// + /// + /// + /// + public async Task UseApiKey( + XOpenActionRequestDto model, + CancellationToken cancellationToken = default + ) + { + // + // Load Model from Request ... + var apiKeyUsage = model.Payload.FromJSON(); + var result = !apiKeyUsage.IsNullOrDefault(); + if (!result) + { + return result; + } + + // + // Reading ApiKey ... + var apiKey = await DbContext.ApiKeys + .FirstOrDefaultAsync(x => x.Id == apiKeyUsage.ApiKeyId); + apiKey.LastUsedAt = DateTime.UtcNow; + DbContext.ApiKeys.Update(apiKey); + await DbContext.SaveChangesAsync(); + + // + // Prepare Entity ... + var entiy = new XApiKeyUsage + { + ApiKeyId = apiKeyUsage.ApiKeyId, + Endpoint = apiKeyUsage.Endpoint, + ClientIP = apiKeyUsage.ClientIP, + HttpMethod = apiKeyUsage.HttpMethod, + StatusCode = apiKeyUsage.StatusCode, + RequestedOn = apiKeyUsage.RequestedOn, + ResponseTimeMs = apiKeyUsage.ResponseTimeMs, + }; + await DbContext.ApiKeyUsages.AddAsync(entiy); + var changes = await DbContext.SaveChangesAsync(); + result = changes > 0; + + // + return result; + } + + /// /// Get User Info For Open Actions ... ///