2013-03-07 167 views
1

我有UserIdentity(用戶,密碼)構造函數的問題。 我的密碼長度爲4個字符。密碼到達服務器時,長度爲36個字符。前4個字符是我的密碼 - 其餘的是隨機垃圾。爲什麼Opc.Ua.UserIdentity不能將密碼乾淨地發送到OPC服務器?

Opc.Ua.Client.dll & Opc.Ua.Core.dll版本爲1.0.238.1。

這是什麼原因導致我該怎麼做才能正確發送密碼?

UPDATE

ApplicationConfiguration configuration = Helpers.CreateClientConfiguration(); 
X509Certificate2 clientCertificate = configuration.SecurityConfiguration.ApplicationCertificate.Find(); 
configuration.CertificateValidator.CertificateValidation += new CertificateValidationEventHandler(CertificateValidator_CertificateValidation); 
EndpointDescription endpointDescription = Helpers.CreateEndpointDescription(Url); 
EndpointConfiguration endpointConfiguration = EndpointConfiguration.Create(configuration); 
endpointConfiguration.OperationTimeout = 300000; 
endpointConfiguration.UseBinaryEncoding = true; 
ConfiguredEndpoint endpoint = new ConfiguredEndpoint(null, endpointDescription, endpointConfiguration); 
BindingFactory bindingFactory = BindingFactory.Create(configuration); 

if (endpoint.UpdateBeforeConnect) 
{ 
    endpoint.UpdateFromServer(bindingFactory); 
    endpointDescription = endpoint.Description; 
    endpointConfiguration = endpoint.Configuration; 
} 

SessionChannel channel = SessionChannel.Create(
    configuration, 
    endpointDescription, 
    endpointConfiguration, 
    bindingFactory, 
    clientCertificate, 
    null); 

m_Session = new Session(channel, configuration, endpoint); 
m_Session.ReturnDiagnostics = DiagnosticsMasks.All; 

m_Session.KeepAlive += new KeepAliveEventHandler(Session_KeepAlive); 
m_Session.Notification += new NotificationEventHandler(m_Session_Notification); 

UserIdentity identity; 
if (userName == null || userName.Length == 0) 
{ 
    identity = new UserIdentity(); 
} 
else 
{ 
    identity = new UserIdentity(userName, password); 
} 

m_Session.Open("ATF UA client", identity); 
log.Debug("Connect ok"); 

回答

1

其餘的不是垃圾的。它應該與您在CreateSessionResponse中發送給OPC UA客戶端的ServerNonce相同。

根據OPC UA說明書中UserIdentityToken加密格式是:

  • 長度 - 字節[4] =>密碼
  • TokenData的長度 - 字節[*] =>您的密碼
  • ServerNonce - 字節[*]

密碼長度36個字節,因爲OPC UA服務器主要使用32字節ServerNonce和你的密碼是4個字節長...

您還應該驗證與該UserIdentityToken一起發送的ServerNonce與您在CreateSessionResponse中提供的相同。

+0

感謝您的信息。我正在使用C#API,我只能假定庫正確處理ServerNonce。我在代碼中添加了一部分代碼。 – paul 2013-03-13 07:49:19

+0

如果您正在編寫服務器,則需要驗證客戶端是否添加了您在CreateSessionResponse中提供的ServerNonce密碼。如果你正在編寫一個客戶端,你必須添加服務器返回的ServerNonce和你的密碼...... – 2013-03-13 08:52:02

+0

你有沒有機會看看我發佈的代碼?我的猜測是,直到最後一行纔有與服務器通信,所以我從哪裏獲得ServerNonce?這肯定會發生在圖書館內。我將DLL作爲西門子軟件包的一部分,它們似乎源於OPC基金會。我搜索了但沒有找到這些庫的任何文檔。再次感謝你的幫助! – paul 2013-03-13 09:45:12

相關問題