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

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

???????

?????? ????? ???? ?? ??????? Microsoft ASP.NET ??? ?? ???? ???? ????????? ??????? ??????? ?? ????? ??? ??????:

Microsoft .NET Framework 1.0

??? ????? ?? .NET Framework 1.0 Service Pack 3 (SP3) ?? ??? ????? ??????? ?????? 817854.????? ?? ?????????? ???? ??? ??? ??????? ?????? ?????? ?? "????? ??????? ?? Microsoft:
817854FIX: ????? ??? ASP.NET ?? ???? ????? ????? ???? ??? ???? ??? ????? ??????

.NET Framework 1.1

??? ????? ?? .NET Framework 1.1 ?????? ????? ?????? Service Pack 1 (SP1) ?? ??? ????? ??????? ?????? 831138.????? ?? ?????????? ???? ??? ??? ??????? ?????? ?????? ?? "????? ??????? ?? Microsoft:
831138FIX: ????? ????? .NET Framework ?????? ??????? System.Net.WebRequest ???? ???? ????? ??? ????? OutOfMemoryException
????? ???? | ?? ????

?? ??? ??????

????? ???

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

??????? ????

????? ????? ????? ???? ????? ????? ???? ??? ???? ???????? HttpWebRequest ????? HttpWebResponse. ?????? ??? ????? ???? ????????? ?? ????? ????? ???? ???????? ??? HttpWebRequest ? ?????? ???? ????? ???????:

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

?????? ??? ????? X 509 ????? ??????? ?? ??? .cer ?? ?? ?????? ????? ClientCertificates.

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

??????? ????????? CryptoAPI ?????? ??? ??????? ?? ???? ???????? ?? ?? ?????? ??? ????? X 509 ??? ??????? ???????? ?? ???? ????????. ?? ????? ????? ??????? ClientCertificates.

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

??? ????? ?? ??????? ASP.NET ???? ?? ?????? ????????? ???????:
  • ??? ????? ????? ?????? ?? ???? ??????? LOCAL_MACHINE ??? ???? ??????? CURRENT_USER ??. ?????? ?? ??? ?? ????? ????? ?????? ???? ??????? ???????:
    1. ???? ??? ???? ?? ???? ??? ????? ????? mmc ?? ???? ??? ?????.
    2. ?? ??????? ???? ???? ??? ?????/????? ???? ??????.
    3. ?? ???? ?????? ?????/????? ???? ?????? ? ???? ??? ?????.
    4. ?? ???? ?????? ????? ???? ?????? ?????? ? ???? ??? ???????? ?? ???? ??? ?????.
    5. ?? ???? ?????? ???????? ????????, ???? ??? ???? ????????? ?? ???? ??? ??????
    6. ?? ???? ?????? ????? ?????????? ???? ??? ?????.
    7. ?? ???? ?????? ????? ???? ?????? ?????? ? ???? ??? ????? ?? ???? ??? ?????.
    8. ?? ?????? ???????? (????????? ??????) ? ?? ?????? ??????? ?? ???? ??? ????????.
    ?? ????? ??????? ??? ?? ??? ??? ????? ??????.
  • ??? ???? ????? ???????? ASP.NET ?????? ???? ??? ??????? ????? ?????? ??????. ???? ???????? ASP.NET ?????? ?????? ??? ??????? ????? ?????? ?????? ??????? ???? WinHttpCertCfg.exe.????? ?? ?????????? ???? ??? ??? ??????? ?????? ?????? ?? "????? ??????? ?? Microsoft:
    823193????? ?????? ??? ????? Windows HTTP 5.1 ?????? ??????
    ?????? ??? ???? ?? ????????? ??? ????? ??????? ??? ?????? ?????? ????? ???? ???? ????? Microsoft (MSDN) ?????? ??? ?????:
    WinHttpCertCfg.exe ? ????? ????? ?????? http://msdn2.microsoft.com/en-us/library/aa384088.aspx

??????? ??? .cer

?????? ?? ??????? ??? ??????? ??????? ?????? ???? ????? ??????? ?? ???? ???? ??? .cer. ??? ?? ??? ???? ??? .cer ????? ??????? Microsoft Internet Explorer ?????? ??? .cer.

???? ???????? ???????? ???????? ?? ???? ?????? ???? ??? ??????? ????? ?????? ??? ????? ?? ??? .cer ??? ????? ??????? ?? class.
//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

??? ??? ??? ?????? ??? ??????? ?? ???? ???????? ?????? ????? CryptoAPI ?????? ??? ??????? ?? ?? ?????? ?? ???? ??? ????? X 509. ??? X509CertificateCollection ????? ???? ???????? ?? ???? ???? ?? ??? ?? ???? ??? X509CertificateCollection.

??? ??? ???? ?? ?????? ??? ????? ????? ??? ????? ??? ????? ?????? ??? ????? ????? ???????? ?????? CertFindCertificateInStore. ?? ????? ??? ?????? ?? ??? Wincrypt.h. ????? ?? ???? ???? ????? ?????? X509CertificateCollection ?????? ??? ??????? ???? ??????.

?????? ????? ????????? ???????? ?????? ??????? ????? ?? ???????? ???? ?? ??????? ?? ???? 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;
		}
	}
}

?????

????? ?? ?????????? ?? ?????? ????? ????? Network (MSDN) ?????? Microsoft ???????:
??? ????? X 509
http://msdn2.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate(vs.71).aspx
?????? ??????? SDK: ?????
http://msdn2.microsoft.com/en-us/library/aa380255.aspx

???????

???? ???????: 895971 - ????? ??? ??????: 01/????? ??????/1428 - ??????: 1.6
????? ???
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
????? ??????: 
kbmt kbhowtomaster kbaspnet kbsample kbcode kbdigitalcertificates kbprogramming kbwebclasses kbhowto kbinfo KB895971 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????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