SSL FORM POST with Client Certificate from ASP.net

Ask a Question related to ASP.NET Security, Design and Development.

  1. #1

    Default 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.
    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;
    }




    Aung Guest

  2. Similar Questions and Discussions

    1. Setting up client certificate.
      I am trying to use a web service over ssl, that requires a client certificate to be installed. i imported a .p12 file and my windows applications...
    2. ASP.Net using a Client Certificate on IIS 6.0
      I have an ASP.Net application application that uses a client certificate to communicate to a third party. Now, in Win2K, to install the Class 1...
    3. Client Side Certificate
      Hi, Regarding Microsoft Knowledge Base Article : 315588, We have 60 clients for our ASP.NET application. Do we need to buy an SSL Key from...
    4. How to mimic client-side form post to .php page
      I was wondering if there was a way I could write a script to change my user profile/info on a site using an html form inside a .php page without...
    5. client certificate
      Hi, I have this problem: I use windows 2003 iis6 and framework. I installed some web service with client certification required but if i ask the...
  3. #2

    Default Re: SSL FORM POST with Client Certificate from ASP.net

    Hi Aung,

    Move the code that performs the post to a serviced component. Configure the
    component to run under the account which has installed the client
    certificate. It should work fine.

    Subra
    "Aung" <aungkyawmoe@hotmail.com> wrote in message
    news:OKXLkDijDHA.2456@TK2MSFTNGP09.phx.gbl...
    > 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;
    > }
    >
    >
    >
    >

    Subra Mallampalli Guest

  4. #3

    Default Re: SSL FORM POST with Client Certificate from ASP.net

    Aung:
    Apply the ASP.NET hotfix (v1.0 [url]http://support.microsoft.com/?id=817854[/url]).
    There is a hot fix for v1.1 and Windows 2003 as well.
    Then give the ASPNET account access to the store with a tool like
    winhttpcertmgr. With this approach you do not need to create a service
    component.

    Hope that helps, if you need more help just drop me a line. I have a doc on
    the issue as well.

    Thanks,

    Norm.


    "Aung" <aungkyawmoe@hotmail.com> wrote in message
    news:OKXLkDijDHA.2456@TK2MSFTNGP09.phx.gbl...
    > 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;
    > }
    >
    >
    >
    >

    Norman Headlam Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139