с# WCF отправляем сообщения

Сначала конечно же генерируем Service client . Для этого заходим в ServiceReference и там жмем "Добавить ссылку на службу"

Далее мы можем отправить сообщение оборачивая в Envelope


ServiceReference1.ServiceSAClient service = new ServiceReference1.ServiceSAClient();
using (FileStream stream = new FileStream(textBox1.Text, FileMode.Open))
{
         mlReader reader = XmlReader.Create(stream);
         MessageVersion ver = MessageVersion.CreateVersion(EnvelopeVersion.Soap11, AddressingVersion.None);
         System.ServiceModel.Channels.Message m = service.async_getMessage(System.ServiceModel.Channels.Message.CreateMessage(ver, "http://tempuri.org/IServiceWCF/Message", reader));
}


Или не оборачивая в Envelope
XmlDocument d = new XmlDocument();
            d.PreserveWhitespace = false;
            d.Load(@"send.xml");
            MemoryStream ms = new MemoryStream();
            XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(ms);
            d.WriteTo(writer);
            writer.Flush();
 
            ms.Position = 0;

MessageVersion ver = MessageVersion.CreateVersion(EnvelopeVersion.Soap11, AddressingVersion.None);
System.ServiceModel.Channels.Message msend = System.ServiceModel.Channels.Message.CreateMessage(XmlDictionaryReader.CreateTextReader(ms, XmlDictionaryReaderQuotas.Max), int.MaxValue, ver);
msend.Headers.Action = "http://tempuri.org/IServiceWCF/Message";
System.ServiceModel.Channels.Message m =service.async_getMessage(msend);


Также я как то писал как ответное сообщения от сервиса сохранить в файл. Есть другое решение, более короткое.


System.ServiceModel.Channels.Message m =service.async_getMessage(msend);
                    MessageBuffer buf = m.CreateBufferedCopy(int.MaxValue);
 
 
                    using (var msstream = new MemoryStream())
                    {
                        using (FileStream file = new FileStream("WCF Responce.xml", FileMode.Create, System.IO.FileAccess.Write))
                        {
                            buf.WriteMessage(file);
                            file.Flush();
                        }
                    }

c# BLToolKit Основы или пошагово

Решил сделать инструкцию с картинками как работать с BLToolKit.
1 Скачать BLToolKit отсюда http://bltoolkit.net/Download.ashx . Качайте архив bltoolkit_dev.zip
2 Далее создаем проект в Visual Studio. Я создавал WinForms проект
3 Вот такие ссылки добавляем

4 Видим там есть папка Templates. Её можно взять из скачанного архива в bltoolkit.4.0.dev\Source\Templates

5 Далее добавляем файл *.tt . Это можно сделать если ткнуть по обозревателю решений. Там Добавить->Создать Элемент. В разделе "Код" есть соответствующий шаблон.

6 Далее заходим в Model1.tt . Там правим следующим образом (В моем случае модел генерируется для MSSQL):
<#@ template language="C#v3.5"         #>
<#@ output extension=".generated.cs"   #>
<#@ include file="TemplatesBLToolkit.ttinclude" #>
<#@ include file="TemplatesMSSQL.ttinclude"     #>
<#
    ConnectionString = @"Server=SQLEXPRESS;Database=ServerDB;Uid=myuser;Pwd=mypas";
 
    Namespace        = "Templates";
    DataContextName  = "DataModel";
 
    GenerateModel();
#>


7 Далее жмем компилировать проект и у нас генерируется необходимый класс, с которым мы уже и будем работать

Примеров работы со сгенерированным классом куча(Например вот), так что главное начало. Ссылка для скачивания примера данного проекта находится снизу.

https://www.box.com/s/bwkrhvfpvxbzjxvitdzp


с# Установить список отзыва CRL для сертификата X509 Certificate

Пропишите using, все не нужны, но данных хватит. Чистить этот список можно бы, но да ладно это не главное в данном посте

using System;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

Пишем всякое вспомогательное


        public struct CRYPTUI_WIZ_IMPORT_SRC_INFO
        {
            public Int32 dwSize;
            public Int32 dwSubjectChoice;
            [MarshalAs(UnmanagedType.LPWStr)]
            public String pwszFileName;
            public Int32 dwFlags;
            [MarshalAs(UnmanagedType.LPWStr)]
            public String pwszPassword;
        }
 
        [DllImport("CryptUI.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern Boolean CryptUIWizImport(
            Int32 dwFlags,
            IntPtr hwndParent,
            IntPtr pwszWizardTitle,
            ref CRYPTUI_WIZ_IMPORT_SRC_INFO pImportSrc,
            IntPtr hDestCertStore
        );
 
        public const Int32 CRYPTUI_WIZ_IMPORT_SUBJECT_FILE = 1;
        public const Int32 CRYPT_EXPORTABLE = 0x00000001;
        public const Int32 CRYPT_USER_PROTECTED = 0x00000002;
        public const Int32 CRYPTUI_WIZ_NO_UI = 0x0001;


А теперь как это всё использовать


CRYPTUI_WIZ_IMPORT_SRC_INFO importSrc = new CRYPTUI_WIZ_IMPORT_SRC_INFO();
            importSrc.dwSize = Marshal.SizeOf(importSrc);
            importSrc.dwSubjectChoice = CRYPTUI_WIZ_IMPORT_SUBJECT_FILE;
            importSrc.pwszFileName = "Cert.crl";
            importSrc.pwszPassword = null;
            //bool z = File.Exists("C:\CenterInformMskf (1).crl");
            importSrc.dwFlags = CRYPT_EXPORTABLE | CRYPT_USER_PROTECTED;
 
            if (!CryptUIWizImport(
              CRYPTUI_WIZ_NO_UI,
                  IntPtr.Zero,
                  IntPtr.Zero,
                  ref importSrc,
                  IntPtr.Zero
            ))
            {
                MessageBox.Show("Error!");
                return;
            }
 
            MessageBox.Show("OK!");


c# How to Get URL of CRL (Revocation list) from x509 certificate

using section. 


using System;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

Main method is GetBaseCrlURL()

        private const string CERT_CRL_EXTENSION = "2.5.29.31";
        private const string CRL_CRL_EXTENSION = "2.5.29.46";
 
        public static string GetBaseCrlUrl(X509Certificate2 cert)
        {
            try
            {
                return (from X509Extension extension in cert.Extensions
                        where extension.Oid.Value.Equals(CERT_CRL_EXTENSION)
                        select GetCrlUrlFromExtension(extension)).Single();
            }
            catch
            {
                return null;
            }
        }
 
        private static string GetDeltaCrlUrl(WinCrypt32.CRL_INFO stCrlInfo)
        {
            IntPtr rgExtension = stCrlInfo.rgExtension;
            X509Extension deltaCrlExtension = null;
 
            for (int i = 0; i < stCrlInfo.cExtension; i++)
            {
                WinCrypt32.CERT_EXTENSION stCrlExt = (WinCrypt32.CERT_EXTENSION)Marshal.PtrToStructure(rgExtension, typeof(WinCrypt32.CERT_EXTENSION));
 
                if (stCrlExt.Value.pbData != IntPtr.Zero && stCrlExt.pszObjId == CRL_CRL_EXTENSION)
                {
                    byte[] rawData = new byte[stCrlExt.Value.cbData];
                    Marshal.Copy(stCrlExt.Value.pbData, rawData, 0, rawData.Length);
                    deltaCrlExtension = new X509Extension(stCrlExt.pszObjId, rawData, stCrlExt.fCritical);
                    break;
                }
 
                rgExtension = (IntPtr)((Int32)rgExtension + Marshal.SizeOf(typeof(WinCrypt32.CERT_EXTENSION)));
            }
            if (deltaCrlExtension == null)
            {
                return null;
            }
            return GetCrlUrlFromExtension(deltaCrlExtension);
        }
 
        private static string GetCrlUrlFromExtension(X509Extension extension)
        {
            try
            {
                Regex rx = new Regex("http://.*crl");
                string raw = new AsnEncodedData(extension.Oid, extension.RawData).Format(false);
                return rx.Match(raw).Value;
            }
            catch
            {
                return null;
            }
        }

It's class helper WinCrypt32



public static class WinCrypt32
    {
        #region APIs
 
        [DllImport("CRYPT32.DLL", EntryPoint = "CryptQueryObject", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern Boolean CryptQueryObject(
            Int32 dwObjectType,
            IntPtr pvObject,
            Int32 dwExpectedContentTypeFlags,
            Int32 dwExpectedFormatTypeFlags,
            Int32 dwFlags,
            IntPtr pdwMsgAndCertEncodingType,
            IntPtr pdwContentType,
            IntPtr pdwFormatType,
            ref IntPtr phCertStore,
            IntPtr phMsg,
            ref IntPtr ppvContext
            );
 
        [DllImport("CRYPT32.DLL", EntryPoint = "CertFreeCRLContext", SetLastError = true)]
        public static extern Boolean CertFreeCRLContext(
            IntPtr pCrlContext
        );
 
        [DllImport("CRYPT32.DLL", EntryPoint = "CertNameToStr", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern Int32 CertNameToStr(
            Int32 dwCertEncodingType,
            ref CRYPTOAPI_BLOB pName,
            Int32 dwStrType,
            StringBuilder psz,
            Int32 csz
        );
 
        [DllImport("CRYPT32.DLL", EntryPoint = "CertFindExtension", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr CertFindExtension(
            [MarshalAs(UnmanagedType.LPStr)]String pszObjId,
            Int32 cExtensions,
            IntPtr rgExtensions
        );
 
        [DllImport("CRYPT32.DLL", EntryPoint = "CryptFormatObject", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern Boolean CryptFormatObject(
            Int32 dwCertEncodingType,
            Int32 dwFormatType,
            Int32 dwFormatStrType,
            IntPtr pFormatStruct,
            [MarshalAs(UnmanagedType.LPStr)]String lpszStructType,
            IntPtr pbEncoded,
            Int32 cbEncoded,
            StringBuilder pbFormat,
            ref Int32 pcbFormat
        );
 
        #endregion APIs
 
        #region Structs
        [StructLayout(LayoutKind.Sequential)]
        public struct CRYPT_OBJID_BLOB
        {
            public uint cbData;
            [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)]
            public byte[] pbData;
        }
 
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct CERT_PUBLIC_KEY_INFO
        {
            public CRYPT_ALGORITHM_IDENTIFIER Algorithm;
            public CRYPTOAPI_BLOB PublicKey;
        }
 
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        public struct CERT_EXTENSION
        {
            [MarshalAs(UnmanagedType.LPStr)]
            public string pszObjId;
            public bool fCritical;
            public CRYPTOAPI_BLOB Value;
        }
 
        [StructLayout(LayoutKind.Sequential)]
        public struct CERT_CONTEXT
        {
            public uint dwCertEncodingType;
            public IntPtr pbCertEncoded;
            public uint cbCertEncoded;
            public IntPtr pCertInfo;
            public IntPtr hCertStore;
        }
 
        public struct CERT_INFO
        {
            public int dwVersion;
            public CRYPTOAPI_BLOB SerialNumber;
            public CRYPT_ALGORITHM_IDENTIFIER SignatureAlgorithm;
            public CRYPTOAPI_BLOB Issuer;
            public FILETIME NotBefore;
            public FILETIME NotAfter;
            public CRYPTOAPI_BLOB Subject;
            public CERT_PUBLIC_KEY_INFO SubjectPublicKeyInfo;
            public CRYPTOAPI_BLOB IssuerUniqueId;
            public CRYPTOAPI_BLOB SubjectUniqueId;
            public int cExtension;
            public CERT_EXTENSION rgExtension;
        }
 
        [StructLayout(LayoutKind.Sequential)]
        public struct CRL_CONTEXT
        {
            public Int32 dwCertEncodingType;
            public IntPtr pbCrlEncoded;
            public Int32 cbCrlEncoded;
            public IntPtr pCrlInfo;
            public IntPtr hCertStore;
        }
 
        [StructLayout(LayoutKind.Sequential)]
        public struct CRL_INFO
        {
            public Int32 dwVersion;
            public CRYPT_ALGORITHM_IDENTIFIER SignatureAlgorithm;
            public CRYPTOAPI_BLOB Issuer;
            public FILETIME ThisUpdate;
            public FILETIME NextUpdate;
            public Int32 cCRLEntry;
            public IntPtr rgCRLEntry;
            public Int32 cExtension;
            public IntPtr rgExtension;
        }
 
        [StructLayout(LayoutKind.Sequential)]
        public struct CRYPT_ALGORITHM_IDENTIFIER
        {
            [MarshalAs(UnmanagedType.LPStr)]
            public String pszObjId;
            public CRYPTOAPI_BLOB Parameters;
        }
 
        [StructLayout(LayoutKind.Sequential)]
        public struct CRYPTOAPI_BLOB
        {
            public Int32 cbData;
            public IntPtr pbData;
        }
 
        [StructLayout(LayoutKind.Sequential)]
        public struct FILETIME
        {
            public Int32 dwLowDateTime;
            public Int32 dwHighDateTime;
        }
 
        [StructLayout(LayoutKind.Sequential)]
        public struct CRL_ENTRY
        {
            public CRYPTOAPI_BLOB SerialNumber;
            public FILETIME RevocationDate;
            public Int32 cExtension;
            public IntPtr rgExtension;
        }
 
        #endregion Structs
 
        #region Consts
 
        public const Int32 CERT_QUERY_OBJECT_FILE = 0x00000001;
        public const Int32 CERT_QUERY_OBJECT_BLOB = 0x00000002;
        public const Int32 CERT_QUERY_CONTENT_CRL = 3;
        public const Int32 CERT_QUERY_CONTENT_FLAG_CRL = 1 << CERT_QUERY_CONTENT_CRL;
        public const Int32 CERT_QUERY_FORMAT_BINARY = 1;
        public const Int32 CERT_QUERY_FORMAT_BASE64_ENCODED = 2;
        public const Int32 CERT_QUERY_FORMAT_ASN_ASCII_HEX_ENCODED = 3;
        public const Int32 CERT_QUERY_FORMAT_FLAG_BINARY = 1 << CERT_QUERY_FORMAT_BINARY;
        public const Int32 CERT_QUERY_FORMAT_FLAG_BASE64_ENCODED = 1 << CERT_QUERY_FORMAT_BASE64_ENCODED;
        public const Int32 CERT_QUERY_FORMAT_FLAG_ASN_ASCII_HEX_ENCODED = 1 << CERT_QUERY_FORMAT_ASN_ASCII_HEX_ENCODED;
        public const Int32 CERT_QUERY_FORMAT_FLAG_ALL = CERT_QUERY_FORMAT_FLAG_BINARY | CERT_QUERY_FORMAT_FLAG_BASE64_ENCODED | CERT_QUERY_FORMAT_FLAG_ASN_ASCII_HEX_ENCODED;
 
        public const Int32 X509_ASN_ENCODING = 0x00000001;
        public const Int32 PKCS_7_ASN_ENCODING = 0x00010000;
 
        public const Int32 X509_NAME = 7;
 
        public const Int32 CERT_SIMPLE_NAME_STR = 1;
        public const Int32 CERT_OID_NAME_STR = 2;
        public const Int32 CERT_X500_NAME_STR = 3;
 
        public const String szOID_CRL_REASON_CODE = "2.5.29.21";
 
        public enum Disposition : uint
        {
            CERT_STORE_ADD_NEW = 1,
            CERT_STORE_ADD_USE_EXISTING = 2,
            CERT_STORE_ADD_REPLACE_EXISTING = 3,
            CERT_STORE_ADD_ALWAYS = 4,
            CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES = 5,
            CERT_STORE_ADD_NEWER = 6,
            CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES = 7,
        }
 
        [Flags]
        public enum FindFlags : int
        {
            CRL_FIND_ISSUED_BY_AKI_FLAG = 0x1,
            CRL_FIND_ISSUED_BY_SIGNATURE_FLAG = 0x2,
            CRL_FIND_ISSUED_BY_DELTA_FLAG = 0x4,
            CRL_FIND_ISSUED_BY_BASE_FLAG = 0x8,
        }
 
        public enum FindType : int
        {
            CRL_FIND_ANY = 0,
            CRL_FIND_ISSUED_BY = 1,
            CRL_FIND_EXISTING = 2,
            CRL_FIND_ISSUED_FOR = 3
        }
 
        #endregion
    }

c# System.ServiceModel.Channels.Message to file

Можно сообщение прочитать через Message.ToString(), но так неверно оно будет скопировано и если вдруг вы при этом подписываете сообщение, то может нарушиться подпись. Данный способ был рабочим для меня.
                    
using (var msstream = new MemoryStream())
                    {
                        var xmlWriter = XmlDictionaryWriter.CreateTextWriter(msstream);
 
                        m.WriteMessage(xmlWriter);
 
                        xmlWriter.Flush();
                        msstream.Position = 0;
                        using (FileStream file = new FileStream("Responce.xml", FileMode.Create, System.IO.FileAccess.Write))
                        {
                            byte[] bytes = new byte[msstream.Length];
                            msstream.Read(bytes, 0, (int)msstream.Length);
                            file.Write(bytes, 0, bytes.Length);
                        }
                    }

с# Добавляем сертфикат в "Доверенные корневые центры сертификации"

void InstallRootCert(string certFileName)
{
    X509Certificate2 certificate = new X509Certificate2(cerFileName);
    X509Store store = new X509Store(StoreName.TrustedPublisher, StoreLocation.LocalMachine);

    store.Open(OpenFlags.ReadWrite);
    store.Add(certificate);
    store.Close();
}

c# проверка email

public static bool checkEmailString(string val)
        {
            if (val.Trim() != "")
            {
                System.Text.RegularExpressions.Match rex = 
                    System.Text.RegularExpressions.Regex.Match(val.Trim(),
                    "^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,3})$", 
                    System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                if (!rex.Success)
                    return false;
                else
                    return true;
            }
            else
                return false;
        }