Ask a Question related to ASP.NET Security, Design and Development.
-
Aung #1
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
-
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... -
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... -
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... -
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... -
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... -
Subra Mallampalli #2
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...cert,> 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..NET> 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;> 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
-
Norman Headlam #3
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...cert,> 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..NET> 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;> 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



Reply With Quote

