2016-05-30 133 views
-1

我試圖消耗第三方Web服務(在俄語語言詳細描述)https://92.46.122.150:8443/esf-web/ws/SessionService?wsdl如何在Visual Studio中將Soap Header傳遞給SOAP WCF服務?

我試圖連接到使用方法「了createSession」網站,獲得一個網站ID會話和關閉會話。 做到這一點我使用Visual Studio 2013,C#,.NET 4.5,WSE 3.0(Microsoft.Web.Services3.dll)。

我已經在Visual Studio 2013「Windows窗體應用程序」中創建了項目。在這種形式下,用戶可以輸入用戶名,登錄名和選擇他的數字簽名證書。此外,我將Web服務添加爲「Web服務參考」,但我不確定如何傳遞標頭的憑據。

我試圖做到這一點:

string strPasswordCertificate = "123456"; 
string strCertificate = "C:/Test/AUTH_RSA_db79bb07b4722c042e025979b3b11995fc46765b.p12"; 
X509Certificate x509_CertAUTH = new X509Certificate(strCertificateFilePathAUTH, strPasswordCertificate); 
string strCertAUTH = x509_CertAUTH.ToString(); 

CreateSessionRequest CreateReq = new CreateSessionRequest(); 

CreateReq.x509Certificate = strCertAUTH; 
string strIIN = "753159846249"; 

CreateReq.tin = strIIN; 
WS.createSession(CreateReq); 

但運行時PROGRAMM的Visual Studio顯示上線錯誤 「WS.createSession(CreateReq)」 象下面這樣:

SoapHeaderException未處理。 System.Web.Services.dll中發生未經處理的「System.Web.Service.Protocols.SoapHeaderExeption」類型異常 附加信息:處理「wsse:Security」標頭時發現錯誤。

當通過SOAP調用XML Web服務方法並在處理SOAP標頭期間發生異常時引發此異常。 之後,我在我的代碼所做的更改如下圖所示:

string strPasswordCertificate = "123456"; 
string strCertificate = "C:/Test/AUTH_RSA_db79bb07b4722c042e025979b3b11995fc46765b.p12"; 
X509Certificate x509_CertAUTH = new X509Certificate(strCertificateFilePathAUTH, strPasswordCertificate); 
string strCertAUTH = x509_CertAUTH.ToString(); 

CreateSessionRequest CreateReq = new CreateSessionRequest(); 

CreateReq.x509Certificate = strCertAUTH; 
string strIIN = "753159846249"; 

CreateReq.tin = strIIN; 
WS.createSession(CreateReq); 

string _userName = "123456789011"; 
string _UserPassword = "TestPass123"; 

UsernameToken userToken; 

userToken = new UsernameToken(_userName, _UserPassword, PasswordOption.SendPlainText); 

SessionService WS = new SessionService(); 
SoapContext requestContext = WS.RequestSoapContext; 
requestContext.Security.Tokens.Add(userToken); 

WS.createSession(CreateReq); 

但隨着運行線路上的PROGRAMM Visual Studio中顯示錯誤時,「WS.createSession(CreateReq)」象下面這樣:

SoapHeaderException是未處理。 System.Web.Services.dll中發生未經處理的「System.Web.Service.Protocols.SoapHeaderExeption」類型異常 附加信息:提供了無效的安全令牌。

我需要更多的工作或改變我的代碼,web崇敬開始工作?任何想法?

回答

0

首先,需要添加網絡服務「https://92.46.122.150:8443/esf-web/ws/SessionService?wsdl」,如服務參考。

其次,您的證書應該像受信任。如果沒有,那麼你可以做下面這樣使用代碼:

System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 

三,加入如C#類解決方案的新項目的SOAP頭。在這個類是768,16代碼象下面這樣:

namespace WindowsFormsApplication1 
{ 
    public class MySoapSecurityHeader : MessageHeader 
    { 
     private readonly UsernameToken _usernameToken; 

     public MySoapSecurityHeader(string username, string password) 
     { 
      _usernameToken = new UsernameToken(string.Empty, username, password); 
     } 

     public MySoapSecurityHeader(string id, string username, string password) 
     { 
      _usernameToken = new UsernameToken(id, username, password); 
     } 

     public override string Name 
     { 
      get { return "Security"; } 
     } 

     public override string Namespace 
     { 
      get { return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; } 
     } 

     protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion) 
     { 
      XmlSerializer serializer = new XmlSerializer(typeof(UsernameToken)); 
      serializer.Serialize(writer, _usernameToken); 
     } 
    } 


    [XmlRoot(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")] 
    public class UsernameToken 
    { 
     public UsernameToken() 
     { 
     } 

     public UsernameToken(string id, string username, string password) 
     { 
      Id = id; 
      Username = username; 
      Password = new Password() { Value = password }; 
     } 

     [XmlAttribute(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")] 
     public string Id { get; set; } 

     [XmlElement] 
     public string Username { get; set; } 

     [XmlElement] 
     public Password Password { get; set; } 
    } 

    public class Password 
    { 
     public Password() 
     { 
      Type = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"; 
     } 

     [XmlAttribute] 
     public string Type { get; set; } 

     [XmlText] 
     public string Value { get; set; } 
    } 
} 

之後,你需要在一個文件中「Form1.cs的」添加代碼:

//The path to the certificate. 
string certPath = "C:/AUTH_RSA256_e9f5afab50193175883774ec07bac05cb8c9e2d7.p12"; 

//Password to signing a certificate 
string certPassword = "123456"; 

//IIN or BIN persom who signing ESF on esf_gov site 
var tin = "123456789021"; 

//Load the certificate into an X509Certificate object. 
X509Certificate x509Cert = new X509Certificate(certPath, certPassword); 

//Transfer sertificate to string value of base64 
var certPEM = ExportToPEM(x509Cert); 


using (SessionServiceClient client = new SessionServiceClient()) 
      { 
       using (new OperationContextScope(client.InnerChannel)) 
       { 
        OperationContext.Current.OutgoingMessageHeaders.Add(
         new MySoapSecurityHeader("123456789011", "TestPass123")); 

        //Create session for a work with site ESF 
        CreateSessionRequest createSessionRequest = new CreateSessionRequest 
        { 
         tin = tin, 
         x509Certificate = certPEM 
        }; 

        var response = client.createSession(createSessionRequest); 
        MessageBox.Show("Session ID is: " + response.sessionId, "Information message", 
            MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1); 

        //Close session for a work with site ESF 
        CloseSessionRequest closeSessionRequest = new CloseSessionRequest 
        { 
         sessionId = response.sessionId, 
         tin = tin, 
         x509Certificate = certPEM 
        }; 

        var closeResponse = client.closeSession(closeSessionRequest); 
       } 
      } 

     } 
     public static string ExportToPEM(X509Certificate cert) 
     { 
      //Export certificate, get baty array, convert in base64 string 
      return Convert.ToBase64String(cert.Export(X509ContentType.Cert), Base64FormattingOptions.InsertLineBreaks); 
     } 

然後運行PROGRAMM,這將是工作。