Created
October 7, 2025 15:55
-
-
Save uzbekdev1/352dbd9cd78537cf0371f5eacb172fad to your computer and use it in GitHub Desktop.
app serial
This file contains hidden or 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
| [ApiController] | |
| [Route("api/[controller]/[action]")] | |
| [Produces("application/json")] | |
| public abstract class BaseController : ControllerBase | |
| { | |
| [FromHeader(Name = DeviceProperties.DEVICE_TYPE_KEY)] | |
| [DefaultValue(DeviceCodes.Browser)] | |
| public string DeviceType { get; set; } | |
| } |
This file contains hidden or 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
| public class CheckDeviceFilter : IResourceFilter | |
| { | |
| private readonly AppSettings _appSettings; | |
| public CheckDeviceFilter(AppSettings appSettings) | |
| { | |
| _appSettings = appSettings; | |
| } | |
| public void OnResourceExecuted(ResourceExecutedContext context) | |
| { | |
| } | |
| public void OnResourceExecuting(ResourceExecutingContext context) | |
| { | |
| if (!context.HttpContext.Request.Headers.TryGetValue(DeviceProperties.APP_SERIAL_KEY, out var serialId) || | |
| serialId != _appSettings.SerialId) | |
| { | |
| throw new Exception("Access is denied"); | |
| } | |
| } | |
| } |
This file contains hidden or 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
| public static class DeviceProperties | |
| { | |
| public const string APP_SERIAL_KEY = "X-APP-SERIAL"; | |
| public const string LANG_CODE_KEY = "X-LANG-CODE"; | |
| public const string DEVICE_TYPE_KEY = "X-DEVICE-TYPE"; | |
| } |
This file contains hidden or 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
| builder.Services.AddSingleton<CheckDeviceFilter>(); | |
| builder.Services.AddSwaggerGen(options => | |
| { | |
| options.OperationFilter<SerialOperationParameter>(); | |
| }); |
This file contains hidden or 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
| public class SerialOperationParameter : IOperationFilter | |
| { | |
| public void Apply(OpenApiOperation operation, OperationFilterContext context) | |
| { | |
| if (context.MethodInfo.DeclaringType == null) | |
| { | |
| return; | |
| } | |
| var serialAttr = context.MethodInfo.GetCustomAttribute<ServiceFilterAttribute>() ?? context.MethodInfo.DeclaringType.GetCustomAttribute<ServiceFilterAttribute>(); | |
| if (serialAttr == null) | |
| { | |
| return; | |
| } | |
| if (serialAttr.ServiceType == typeof(CheckDeviceFilter)) | |
| { | |
| if (operation.Parameters == null) | |
| operation.Parameters = new List<OpenApiParameter>(); | |
| operation.Parameters.Add(new OpenApiParameter | |
| { | |
| Name = DeviceProperties.APP_SERIAL_KEY, | |
| In = ParameterLocation.Header, | |
| Required = true | |
| }); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment