Wednesday, November 24, 2021

Cách serialize và deserialize XML

 public static string XmlSerialize(object ObjectSerialized)

    {
        try
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.NewLineHandling = NewLineHandling.Replace;
            settings.NewLineChars = "\n";
            StringWriter stringWriter = new StringWriter();
            XmlWriter writer = XmlWriter.Create(stringWriter, settings);
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add(string.Empty, string.Empty);
            new XmlSerializer(ObjectSerialized.GetType()).Serialize(writer, ObjectSerialized, namespaces);
            return stringWriter.ToString();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public static object XmlDeserialize(string strXml, Type objType)
    {
        try
        {
            StringReader rd = new StringReader(strXml);
            return new XmlSerializer(objType).Deserialize(rd);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Wednesday, November 10, 2021

Thiết lập ContentType cho Web service Client

 Khi gọi WSDL bằng Web Service client (SoapHttpClientProtocol), nếu gặp lỗi Client found response content type of 'text/xml;charset=utf-8', but expected 'text/xml' là do ContentType mặc định của response là text/xml nhưng response trả về từ đối tác là text/xml;charset=utf-8.

Để khắc phục, trong class service kế thừa từ SoapHttpClientProtocol, bổ sung override hàm GetWriterForMessage để có thể gán lại ContentType cho SoapMessage.


public partial class PartnerAPI : System.Web.Services.Protocols.SoapHttpClientProtocol 

{
    protected override System.Xml.XmlWriter GetWriterForMessage(SoapClientMessage message, int bufferSize)

        {

            message.ContentType = "text/xml;charset=utf-8";

            return base.GetWriterForMessage(message, bufferSize);

        }

}

Wednesday, October 27, 2021

Cách trích xuất RSA private và public key từ file .pfx

 Note: First you will need a linux based operating system that supports openssl command to run the following commands.

  1. Extract the key-pair
    #openssl pkcs12 -in sample.pfx -nocerts -nodes -out sample.key

  2. Get the Private Key from the key-pair
    #openssl rsa -in sample.key -out sample_private.key

  3. Get the Public Key from key pair
    #openssl rsa -in sample.key -pubout -out sample_public.key

  4. Need to do some modification to the private key -> to pkcs8 format
    #openssl pkcs8 -topk8 -inform PEM -in sample_private.key -outform PEM -nocrypt
    Copy the output and save it as sample_private_pkcs8.key

  5. Get those files
    public key: sample_public.key
    private key:  sample_private_pkcs8.key

Nguồn: https://support.citrix.com/article/CTX229158

Friday, October 01, 2021

Cách trích xuất RSA public key từ public certificate

 x509 -pubkey -noout -in C:\Users\tu.dang\Desktop\GoliveVTB\Vietinbank_ITC2020.cer

Monday, September 20, 2021

Convert file .p12 sang .pfx

 Đơn giản chỉ cần đổi tên.

Nguồn: https://akbarahmed.com/2011/11/04/convert-p12-to-pfx/

Thursday, March 11, 2021

Mẫu Regular Expression check định dạng ngày tháng

(years 0-9999) 

^(\w{2})_(\w{1,5})_(?<!\d)(?:(?:(?:1[6-9]|[2-9]\d)?\d{2})(?:(?:(?:0[13578]|1[02])31)|(?:(?:0[1,3-9]|1[0-2])(?:29|30)))|(?:(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))0229)|(?:(?:1[6-9]|[2-9]\d)?\d{2})(?:(?:0?[1-9])|(?:1[0-2]))(?:0?[1-9]|1\d|2[0-8]))(?!\d)_(\w{2,3})(.txt)?$

(years 2000-2099)

(?<!\d)(?:(?:20\d{2})(?:(?:(?:0[13578]|1[02])31)|(?:(?:0[1,3-9]|1[0-2])(?:29|30)))|(?:(?:20(?:0[48]|[2468][048]|[13579][26]))0229)|(?:20\d{2})(?:(?:0?[1-9])|(?:1[0-2]))(?:0?[1-9]|1\d|2[0-8]))(?!\d)