Thursday, October 24, 2024

Tạo public key khi có private key

 rsa -in ./AWSGeneratedKey.pem -pubout -out PublicKey.pub

Monday, April 15, 2024

Ví dụ về Middleware

 namespace EKYC_Core.Middlewares

{

    public class CheckAuthenMiddleware

    {

        private readonly RequestDelegate _next;

        private ILogService? logService;


        public CheckAuthenMiddleware(RequestDelegate next) 

        {

            _next = next;

        }


        public async Task InvokeAsync(HttpContext context)

        {

            //IConfiguration iConfig = context.RequestServices.GetRequiredService<IConfiguration>();

            //List<AuthenConfig> listAuthen = iConfig.GetSection("AuthenConfig")?.Get<List<AuthenConfig>>();


            //IAuthentication authentication = new EKYC_Core.Authentication.Authentication(context.RequestServices, context, listAuthen);

            //try

            //{

            //    await authentication.Validate();

            //}

            //catch (ProjectException ex)

            //{

            //    _ = this.logService.WriteException(ex);

            //    coreResponse = new CoreResponse(ex);

            //    return new JsonResult(coreResponse);

            //}

            //catch (Exception ex)

            //{


            //}





            Stream originalBody = context.Response.Body;


            string requestData = string.Empty;


            try

            {

                var request = context.Request;

                if (request.Method == HttpMethods.Post && request.ContentLength > 0)

                {

                    request.EnableBuffering();

                    var buffer = new byte[Convert.ToInt32(request.ContentLength)];

                    await request.Body.ReadAsync(buffer, 0, buffer.Length);

                    //get body string here...

                    var requestContent = Encoding.UTF8.GetString(buffer);

                    Console.WriteLine(requestContent);

                    request.Body.Position = 0;  //rewinding the stream to 0

                }



                




                using (var memStream = new MemoryStream())

                {

                    context.Response.Body = memStream;


                    await _next(context);


                    memStream.Position = 0;

                    string responseBody = new StreamReader(memStream).ReadToEnd();

                    Console.WriteLine(responseBody);

                    memStream.Position = 0;

                    await memStream.CopyToAsync(originalBody);

                }


            }

            finally

            {

                context.Response.Body = originalBody;

            }



            //var logName = "SYSTEM";

            //this.logService = serviceProvider.GetLogService(logName) ?? serviceProvider.GetLogService("SYSTEM");



            //foreach (var header in context.Request.Headers)

            //{

            //    Console.WriteLine($"Header: {header.Key}: {header.Value}");

            //    //_ = this.logService.WriteMessage($"Header: {header.Key}: {header.Value}");

            //}


            //await _next(context);

        }

    }


    public static class CheckAuthenMiddlewareExtensions

    {

        public static IApplicationBuilder UseCheckAuthen(

            this IApplicationBuilder builder)

        {

            return builder.UseMiddleware<CheckAuthenMiddleware>();

        }

    }

}

Tuesday, September 26, 2023

Flow gọi API SSC Core

 A. Hủy thanh toán: Khi chuyển trạng thái giao dịch sang Hủy. Web sẽ gọi

    1. CheckPaymentInvoiceMasterForBE

    2. ChangePaymentMasterStatusForBE

    3. GetPaymentMasterNoPermissionForBE

    4. GetTransactionStatusForBE

    5. SearchTransaction

B. Màn hình Gán miễn giảm cho học sinh, Nút cập nhật gọi API?

Wednesday, September 20, 2023

Cách trích xuất file public certificate từ file pfx

 openssl pkcs12 -in [yourfile.pfx] -clcerts -nokeys -out [drlive.crt]


Tham khảo: https://www.ibm.com/docs/en/arl/9.7?topic=certification-extracting-certificate-keys-from-pfx-file

Thursday, December 08, 2022

Lệnh xem log trong linux

 

sudo tailf /var/log/messages

Wednesday, November 16, 2022

Trang web convert RSA Key từ XML sang PEM

 https://raskeyconverter.azurewebsites.net/XmlToPem?handler=ConvertPEM

Tuesday, April 19, 2022

Xử lý request POST với application/x-www-form-urlencoded trong WCF Rest

 Khi code WCF Rest mà client submit POST request với dạng application/x-www-form-urlencoded thì code như sau:

Khai báo input đầu vào là kiểu Stream. Sau đó read string ra 1 biến vì khi POST dạng application/x-www-form-urlencoded thì các tham số request sẽ được format kiểu key1=value1&key2=value2

Do đó chỉ cần read ra 1 chuỗi string, sau đó bóc tách từng cặp key value để xử lý.

Nguồn: https://social.msdn.microsoft.com/Forums/vstudio/en-US/2a1b5690-fb2c-4303-a122-fe99d383be7c/how-to-accept-applicationxwwwformurlencoded?forum=wcf

public Stream ProcessArbitraryInput(Stream input)

    {

        string strInput = new StreamReader(input).ReadToEnd();

        string htmlResult = @"<html><head><title>Result</title></head>

<body>

<h1>First: {{first}}</h1>

<h1>Second: {{second}}</h1>

</body></html>";

        Regex inputRegex = new Regex(@"First=([^\&]+)\&Second=(.+)");

        Match match = inputRegex.Match(strInput);

        string first, second;

        if (match.Success)

        {

            first = match.Groups[1].Value.Replace('+'' ');

            second = match.Groups[2].Value.Replace('+'' ');

        }

        else

        {

            first = second = "Error";

        }

        htmlResult = htmlResult.Replace("{{first}}", first).Replace("{{second}}", second);

        WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";

        return new MemoryStream(Encoding.UTF8.GetBytes(htmlResult));

    }