using System.IO; using xCommons.Constants; using Microsoft.AspNetCore.Http; namespace xCommons.Extensions { public static partial class XHttpContextExtensions { /// /// Convert IFormFile to Bytes Array /// /// /// public static byte[] ToByteArray(this IFormFile source) { // byte[] data; using (var br = new BinaryReader(source.OpenReadStream())) { data = br.ReadBytes((int)source.OpenReadStream().Length); } // return data; } /// /// Extract Rate Limit Key ... /// /// /// /// public static string GetXRateLimitKey( this HttpContext source, XRateLimitKeyMode keyMode ) { // if (source.IsNull()) { return "unknown"; } // switch (keyMode) { // case XRateLimitKeyMode.ClientId: { // var clientId = source.Request.Headers[XRateLimitConstants.ClientIdHeader].ToString(); return clientId.IsNullOrEmpty() ? "anonymous" : clientId.ToNormalString(); } // case XRateLimitKeyMode.User: { // var userId = source.User?.FindFirst(XCustomClaims.UserId)?.Value; if (userId.IsNullOrEmpty()) { userId = source.User?.Identity?.Name; } // return userId.IsNullOrEmpty() ? "anonymous" : userId.ToNormalString(); } // case XRateLimitKeyMode.Route: { // return string.Format( "{0}:{1}", source.Request.Method, source.Request.Path ).ToNormalString(); } // case XRateLimitKeyMode.Custom: { return "custom"; } // case XRateLimitKeyMode.Ip: default: { // return source.Connection.RemoteIpAddress .IsNull() ? "unknown" : source.Connection.RemoteIpAddress .ToString() .ToNormalString(); } } } } }