SSL FORM POST with Client Certificate from ASP.net

From: Aung (aungkyawmoe_at_hotmail.com)
Date: 10/09/03


Date: Thu, 9 Oct 2003 11:50:33 +0700

I have a class written to perform FORM POST with Client Certificate and it
works fine with Windows Appication.
But, I am having trouble using it from ASP.NET application and everytime i
am getting "connection cannot be established" error.

Any help?

Aung

Here is the code of my FOR POST class.

//************************************

  public class CertPolicy : ICertificatePolicy
  {
   public bool CheckValidationResult(ServicePoint sp, X509Certificate cert,
    WebRequest request, int problem)
   {
    return true;
   }
  }

  public class myclass
  {
   public byte[] str2ByteArray(string str)
   {
    byte[] barr = new byte[str.Length];
    for (int i=0; i<str.Length; i++)
    {
     barr[i] = Convert.ToByte(str[i]);
    }
    return barr;
   }
  }

  public string postData(string url, string postData)
  {
   string retStr="", tempStr = "";
   HttpWebResponse result = null;
   try
   {
    HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);
    req.Method = "POST";
    req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET
CLR 1.0.3705)";
    req.ContentType = "application/xml; charset=utf-8";
    //req.Headers.Add("charset","utf-8");
    req.ContentLength = postData.Length;
    req.KeepAlive = true;
    req.Timeout = 5000;

    X509Certificate myCert =
     X509Certificate.CreateFromCertFile(@"c:\ccer.der");
    X509CertificateCollection x509 = req.ClientCertificates;
    x509.Add (myCert);
    req.ClientCertificates.Add(myCert);
    ServicePointManager.CertificatePolicy = new CertPolicy();

    byte[] postBytes = null;

    if (postData != null)
    {
     myclass mc = new myclass();
     postBytes = mc.str2ByteArray(postData);
     req.ContentLength = postBytes.Length;
     Stream newStream = req.GetRequestStream();
     newStream.Write(postBytes, 0, postBytes.Length);
     newStream.Close();
    }
    else
    {
     req.ContentLength = 0;
    }

    result = (HttpWebResponse) req.GetResponse();
    Stream ReceiveStream = result.GetResponseStream();
    Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
    StreamReader sr = new StreamReader( ReceiveStream, encode );
    Char[] read = new Char[256];
    int count = sr.Read( read, 0, 256 );

    while (count > 0)
    {
     tempStr = new String(read, 0, count);
     retStr += tempStr;
     count = sr.Read(read, 0, 256);
    }
    retStr.Trim();
   }
   catch (Exception e)
   {
    retStr = e.Message.ToString();
   }
   finally
   {
    if ( result != null )
    {
     result.Close();
    }
   }
   return retStr;
  }



Relevant Pages

  • SSL FORM POST with Client Certificate from ASP.net
    ... I have a class written to perform FORM POST with Client Certificate and it ... works fine with Windows Appication. ... I am having trouble using it from ASP.NET application and everytime i ... public string postData ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • SSL FORM POST with Client Certificate from ASP.net
    ... I have a class written to perform FORM POST with Client Certificate and it ... works fine with Windows Appication. ... I am having trouble using it from ASP.NET application and everytime i ... public string postData ...
    (microsoft.public.inetserver.iis.security)

Quantcast