-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72c8bda
commit 1530ec0
Showing
66 changed files
with
880 additions
and
583 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 83 additions & 11 deletions
94
01-Core/Jinget.Core/ExtensionMethods/HttpContextExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,94 @@ | ||
namespace Jinget.Core.ExtensionMethods; | ||
using System.Net; | ||
|
||
namespace Jinget.Core.ExtensionMethods; | ||
|
||
public static class HttpContextExtensions | ||
{ | ||
/// <summary> | ||
/// Check if given request is a multipart request or not | ||
/// Determines whether the current HTTP request has a multipart content type (specifically "multipart/form-data"). | ||
/// </summary> | ||
public static bool IsMultipartContentType(this HttpContext context) => | ||
context.Request.GetTypedHeaders().ContentType != null && | ||
context.Request.GetTypedHeaders().ContentType.MediaType.Value.ToLower().StartsWith("multipart/form-data"); | ||
/// <param name="context">The HttpContext representing the current HTTP request.</param> | ||
/// <returns>True if the content type is multipart/form-data; otherwise, false.</returns> | ||
/// <remarks> | ||
/// This method checks the Content-Type header of the request to determine if it starts with "multipart/form-data". | ||
/// It uses the GetTypedHeaders() extension method for robust header parsing and performs a case-insensitive comparison. | ||
/// </remarks> | ||
public static bool IsMultipartContentType(this HttpContext context) | ||
{ | ||
// Retrieve the parsed Content-Type header from the request. | ||
var contentTypeHeader = context.Request.GetTypedHeaders().ContentType; | ||
|
||
// Check if the Content-Type header exists. | ||
if (contentTypeHeader != null) | ||
{ | ||
// Retrieve the media type value from the parsed header. | ||
var mediaType = contentTypeHeader.MediaType.Value; | ||
|
||
// Check if the media type value exists and starts with "multipart/form-data" (case-insensitive). | ||
if (mediaType != null && mediaType.StartsWith("multipart/form-data", StringComparison.CurrentCultureIgnoreCase)) | ||
{ | ||
return true; // It's a multipart/form-data request. | ||
} | ||
} | ||
|
||
return false; // It's not a multipart/form-data request. | ||
} | ||
|
||
/// <summary> | ||
/// Get request connecton ip address | ||
/// Retrieves the client's IP address from the HttpContext. | ||
/// </summary> | ||
public static string GetIpAddress(this HttpContext context) => | ||
context.Connection.RemoteIpAddress == null | ||
? "Unknown" | ||
: context.Connection.RemoteIpAddress.ToString(); | ||
/// <param name="context">The HttpContext from which to retrieve the IP address.</param> | ||
/// <param name="customClientIpHeader"> | ||
/// The header name containing the client's IP address (e.g., "X-Forwarded-For"). | ||
/// Defaults to "X-Forwarded-For". | ||
/// </param> | ||
/// <returns> | ||
/// The client's IP address as a string, or "Unknown" if the IP address cannot be determined. | ||
/// </returns> | ||
/// <remarks> | ||
/// This method first attempts to retrieve the IP address from the specified custom header. | ||
/// If the header is not found or the IP address in the header is invalid, it falls back to the | ||
/// RemoteIpAddress property of the HttpContext.Connection. | ||
/// The RemoteIpAddress property represents the IP address of the immediate connection to the server. | ||
/// In environments with reverse proxies or load balancers, the RemoteIpAddress may not represent | ||
/// the actual client's IP address. | ||
/// The method also validates the IP address retrieved from the custom header using IPAddress.TryParse. | ||
/// </remarks> | ||
public static string GetClientIpAddress(this HttpContext context, string customClientIpHeader = "X-Forwarded-For") | ||
{ | ||
// Get the IP address of the immediate connection to the server. | ||
// This may be the proxy or load balancer's IP, not the client's. | ||
var ipAddress = context.Connection.RemoteIpAddress?.ToString(); | ||
|
||
// Check if the specified custom header contains the client's IP address. | ||
if (context.Request.Headers.TryGetValue(customClientIpHeader, out var clientIpAddress)) | ||
{ | ||
// Get the first IP address from the header (in case of multiple IPs). | ||
var firstIp = clientIpAddress.ToString().Split(',')[0].Trim(); | ||
|
||
public static bool EndpointIsAuthorized(this HttpContext httpContext) | ||
// Validate the IP address format. | ||
if (IPAddress.TryParse(firstIp, out _)) | ||
{ | ||
// If valid, use the IP address from the header. | ||
ipAddress = firstIp; | ||
} | ||
} | ||
|
||
// Return the retrieved IP address, or "Unknown" if not found. | ||
return ipAddress ?? "Unknown"; | ||
} | ||
|
||
/// <summary> | ||
/// Checks if the endpoint associated with the current HTTP request has the [Authorize] attribute. | ||
/// </summary> | ||
/// <param name="httpContext">The HttpContext for the current request.</param> | ||
/// <returns>True if the endpoint has the [Authorize] attribute; otherwise, false.</returns> | ||
/// <remarks> | ||
/// This method retrieves the endpoint from the HttpContext and checks for the presence of the AuthorizeAttribute. | ||
/// It returns true if the attribute is found; otherwise, it returns false. | ||
/// This method does not evaluate authorization policies. It only checks for the existence of the attribute. | ||
/// </remarks> | ||
public static bool EndpointIsDecoratedWithAuthorizeAttribute(this HttpContext httpContext) | ||
=> httpContext.GetEndpoint()?.Metadata.GetMetadata<AuthorizeAttribute>() != null; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.