Microsoft Visual C# .NET ??? HttpWebRequest ?? HttpWebResponse ???? ?? ????? ?? ??? ??????? ?????? ???? ?? ????? ?? ??? ???? ????

???? ?????? ???? ??????
???? ID: 895971 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.

??????????

????? ?? ??? ??????? ?????? ???? Microsoft asp.NET ??????????? ??, ?? ????? ?????????? ?? ?????? ??? ??????? ???? ??????:

Microsoft .NET ????? 1.0

.NET Framework 1.0 ?????? ??? 3 (SP3) ?? ??????? ???? ????, ?? 817854 ???????? ?? ??????? ???? ????????? ??????? ?? ???, Microsoft ?????? ??? ??? ???? ????? ?? ??? ????? ???? ?????? ????? ????::
817854FIX: asp.NET ??? ????????? ???? ?????? ??????? ?????? ???? ?? ???????-????????? ??? ???? ?? ???

.NET ????? 1.1

.NET Framework 1.1 ?????? ??? 1 (SP1) ?? ??????? ???? ????, ?? 831138 ???????? ?? ??????? ???? ????????? ??????? ?? ???, Microsoft ?????? ??? ??? ???? ????? ?? ??? ????? ???? ?????? ????? ????::
831138FIX: ?? ??? ???? System.Net.WebRequest ?????? ????? ???? ?? ?? .NET Framework ????????? experiences OutOfMemoryException ?????????
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

?????

?? ???? ?? ????? ?? ??? ??????? ?????? ???? ?? ????? ?? ??? ???? ?? ????? ???? ??HttpWebRequest, ??HttpWebResponseMicrosoft Visual C# NET ??? ?????

???? ???????

?? ??? ????? ?? ????? ?? ?? ?? ???????? ??, ?? ?? ??????? ?????? ???? ?? ??? ???? ???HttpWebRequest??HttpWebResponse????? ??? ??????? ?????? ???? ?? ????? ?? ??? ????? ?? ??? ????? ???? ?? ???? ?? ?????? ???? ?? ??????? ???? ?? ???HttpWebRequest????, ????? ??????? ??? ?? ??? ?? ?????:

?????? 1:

?????X509Certificate?????? ???? ?? ??? .CER ???? ?? ???? ?? ??? ????, ?? ??? ??? ????ClientCertificates????

???? 2

?????????? ?????? ?? ?????????? ??????? ????, ?? ???? ??? ??? ???? ?? ??? CryptoAPI ??? ?? ????? ????X509Certificate?????????? ?????????? ?????? ?? ??????? ???? ?? ??? ????? ???? ??? ??? ?? ???? ???ClientCertificates????

??? ??????? ?????? ???? ?? ????? ?? ??? ??????????

?? ?? asp.NET ??????????? ?? ??? ??? ???? ???, ?? ????????? ???? ?? ????? ?????????? ?? ????? ?????:
  • ??????? ?????? ???? ???? CURRENT_USER ????????? ???? ??? ?? LOCAL_MACHINE ????????? ???? ??? ??????? ???? ?????? ??? ?????? ???? ???? ??????? ?????? ???? ??????? ??, ????? ????? ?? ???? ????:
    1. ????? ????,???????????? ????,???????????:MMC?? ????-????? ????, ?? ???? ???OK.
    2. ????? ???????????? ??,?????/??????? ?????-??.
    3. ??????????/??????? ?????-??????? ????? ???, ????? ????add.
    4. ??????????????? ?????-?? ??????????? ????? ???, ????? ?????????? ?????? ????-????? ????, ?? ???? ???add.
    5. ??????????????? ?????-??????? ????? ???, ????? ???????????? ?????? ????-????? ????, ?? ???? ???next
    6. ????????????? ?? ??? ????????? ????? ???, ????? ??????????.
    7. ??????????????? ?????-?? ??????????? ????? ???, ????? ??????? ?????? ????-????? ????, ?? ???? ???OK.
    8. ??????? ?????????? ???? (??????? ????????), ??????? ??????????????? ????-????? ????, ?? ???? ????????? ????.
    ???? ??? ??? ??????? ?????? ???? ???????? ???? ??????
  • ?? asp.NET ?????????? ?? ??????? ?????????? ?? ??? ???? ????? ?? ??? ???? ?? ?????? ???? ?????? ??? ???? ?? ??? asp.NET ?????????? ???? ????????? ??????? ?????????? ?? ??? ???? ????? ?? ???, WinHttpCertCfg.exe ????? ?? ????? ????????? ??????? ?? ???, Microsoft ?????? ??? ??? ???? ????? ?? ??? ????? ???? ?????? ????? ????::
    823193Windows HTTP 5.1 ?????? ???? ?? ????? ????? ??????? ???? ?? ??? ???? ????
    ?? ????? ?? ????? ???? ???? ?? ???? ??? ???? ??????? ?? ??? ????? Microsoft ?????? ??????? (MSDN) ??? ???? ?? ????:
    WinHttpCertCfg.exe, ?? ?????? ???? ???????????? ?????HTTP://msdn2.Microsoft.com/en-us/library/aa384088.aspx

??? .CER ????? ?? ????? ????

???? 1 ?? ????? ???? ?? ??? ???? ??, ????? ???? ?? ???????? ?? ?? ???? ??? ??? .CER ????? ??? ??? ???? ??? .CER ???? ??????? ???? ??, ?? Microsoft ??????? ?? ????? .CER ????? ?? ??????? ???? ?? ??? Explorer.

????? ??? ?? ??? ????? ?? ???? ??? ?? .CER ????? ?? ??? ?????? ???? ?? ??????? ???? ?? ??? ???? ?? ????? ???? ??HttpWebRequest?????
//Uncomment the following code if you need a proxy. The boolean true is used to bypass the local address.
//WebProxy proxyObject = new WebProxy("Your Proxy value",true); 
//GlobalProxySelection.Select = proxyObject;

// Obtain the certificate. 
try
{
	//You must change the path to point to your .cer file location. 
	X509Certificate Cert = X509Certificate.CreateFromCertFile("C:\\mycert.cer");
	// Handle any certificate errors on the certificate from the server.
	ServicePointManager.CertificatePolicy = new CertPolicy();
	// You must change the URL to point to your Web server.
	HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://YourServer/sample.asp");
	Request.ClientCertificates.Add(Cert);
	Request.UserAgent = "Client Cert Sample";
	Request.Method = "GET";
	HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
	// Print the repsonse headers.
	Console.WriteLine("{0}",Response.Headers);
	Console.WriteLine();
	// Get the certificate data.
	StreamReader sr = new StreamReader(Response.GetResponseStream(), Encoding.Default);
	int count;
	char [] ReadBuf = new char[1024];
	do
	{
		count = sr.Read(ReadBuf, 0, 1024);
		if (0 != count)
		{
			Console.WriteLine(new string(ReadBuf));
		}
						
	}while(count > 0);
}
catch(Exception e)
{
	Console.WriteLine(e.Message);
}
	

//Implement the ICertificatePolicy interface.
class CertPolicy: ICertificatePolicy
{
	public bool CheckValidationResult(ServicePoint srvPoint, 
X509Certificate certificate, WebRequest request, int certificateProblem)
	{
		// You can do your own certificate checking.
		// You can obtain the error values from WinError.h.

		// Return true so that any certificate will work with this sample.
		return true;
	}
}

CryptoAPI ??? ?? ????? ????

If you must obtain the certificate from the certificate store, use the CryptoAPI functions to obtain the certificate, and then store it in anX509Certificateclass object. TheX509CertificateCollectionclass enumerates all the certificates in a store and then puts them in anX509CertificateCollectionclass object.

If you want to obtain a specific certificate, you must change the class code to obtain a specific certificate by using theCertFindCertificateInStore?????? ?? ??? ???? ???.. This function is declared in the Wincrypt.h file. Alternatively, you can enumerate theX509CertificateCollectionfunction to find the certificate that you want.

????? ????? ??? ?? ?? ?????? ??? ???? ?????????? ?? ????? ???? ??CertEnumCertificatesInStore?????? ?? ??? ???? ???..
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Runtime.InteropServices;

namespace SelectClientCert
{
	/// Sample that describes how how to select client cetificate and send it to the server.

	class MyCerts{

		private static int CERT_STORE_PROV_SYSTEM = 10;
		private static int CERT_SYSTEM_STORE_CURRENT_USER = (1 << 16);
		///private static int CERT_SYSTEM_STORE_LOCAL_MACHINE = (2 << 16);

		[DllImport("CRYPT32", EntryPoint="CertOpenStore", CharSet=CharSet.Unicode, SetLastError=true)]
		public static extern IntPtr CertOpenStore(
			int storeProvider, int encodingType,
			int hcryptProv, int flags, string pvPara);

		[DllImport("CRYPT32", EntryPoint="CertEnumCertificatesInStore", CharSet=CharSet.Unicode, SetLastError=true)]
		public static extern IntPtr CertEnumCertificatesInStore(
			IntPtr storeProvider,
			IntPtr prevCertContext);

		[DllImport("CRYPT32", EntryPoint="CertCloseStore", CharSet=CharSet.Unicode, SetLastError=true)]
		public static extern bool CertCloseStore(
			IntPtr storeProvider,
			int flags);
		
		X509CertificateCollection m_certs;

		public MyCerts(){
			m_certs = new X509CertificateCollection();
		}

		public int Init()
		{
			IntPtr storeHandle;
			storeHandle = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_SYSTEM_STORE_CURRENT_USER, "MY");
			IntPtr currentCertContext;
			currentCertContext = CertEnumCertificatesInStore(storeHandle, (IntPtr)0);
			int i = 0;
			while (currentCertContext != (IntPtr)0) 
			{
				m_certs.Insert(i++, new X509Certificate(currentCertContext));
				currentCertContext = CertEnumCertificatesInStore(storeHandle, currentCertContext);
			}
			CertCloseStore(storeHandle, 0);

			return m_certs.Count;
		}
		
		public X509Certificate this [int index]
		{
			get 
			{
				// Check the index limits.
				if (index < 0 || index > m_certs.Count)
					return null;
				else
					return m_certs[index];
			}
		}
	};
	class MyHttpResource
	{
		String m_url;

		public MyHttpResource(string url){
			m_url = url;
		}

		public void GetFile(){

			HttpWebResponse  result = null;

			try{
			
				HttpWebRequest req = (HttpWebRequest)WebRequest.Create(m_url);
				req.Credentials  = CredentialCache.DefaultCredentials;

				///Method1
				//req.ClientCertificates.Add(X509Certificate.CreateFromCertFile("D:\\Temp\\cert\\c1.cer"));
		
				///Method2
				///Uses interop services
				MyCerts mycert = new MyCerts();
				if(mycert.Init() > 0)
					req.ClientCertificates.Add(mycert[0]);

				result = (HttpWebResponse)req.GetResponse();
				
				Stream ReceiveStream = result.GetResponseStream();
				Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

				StreamReader sr = new StreamReader( ReceiveStream, encode );
				Console.WriteLine("\r\nResponse stream received");

				Char[] read = new Char[256];
				int count = sr.Read( read, 0, 256 );

				Console.WriteLine("HTTP Response...\r\n");
				while (count > 0) 
				{
					String str = new String(read, 0, count);
					Console.Write(str);
					count = sr.Read(read, 0, 256);
				}

			} 
			catch(WebException e) 
			{
            
				Console.WriteLine("\r\nError:");
				#if (DEBUG)
					Console.WriteLine(e.ToString());
				#else		
					Console.WriteLine(e.Message); 				
				#endif

			} 
			finally 
			{
				if ( result != null ) {
					result.Close();
				}
			}
				
		}
	
	}

	class CertSample
	{
		static void Main(string[] args)
		{
			try
			{
				if (args.Length < 1)
				{
					Console.WriteLine("No url is entered to download, returning.\n");
					Console.WriteLine("Usage: CertSample <urltoget>\n");
					Console.WriteLine("  e.g: CertSample https://servername \n"); 

					return;
				}

				MyHttpResource hr = new MyHttpResource(args[0]);
				hr.GetFile();
			}
			catch(Exception e)
			{
				Console.WriteLine(e.ToString());
			}
			return;
		}
	}
}

??????

???? ??????? ?? ???, ????? Microsoft ?????? ??????? (MSDN) ??? ?????? ?? ????::
X509Certificate ????
(vs.71) http://msdn2.Microsoft.com/en-us/library/SYSTEM.Security.cryptography.x509certificates.x509certificate .aspx
SDK ??????????: ???????????????
HTTP://msdn2.Microsoft.com/en-us/library/aa380255.aspx

???

???? ID: 895971 - ????? ???????: 05 ?????? 2010 - ??????: 5.0
???? ???? ???? ??:
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
??????: 
kbhowtomaster kbaspnet kbsample kbcode kbdigitalcertificates kbprogramming kbwebclasses kbhowto kbinfo kbmt KB895971 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:895971

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com