FormConnectSelect.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Opc.Ua;
  2. using Opc.Ua.Configuration;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Security.Cryptography.X509Certificates;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace OpcUaHelper.Forms
  15. {
  16. public partial class FormConnectSelect : Form
  17. {
  18. public FormConnectSelect( OpcUaClient opcUaClient )
  19. {
  20. InitializeComponent( );
  21. this.m_OpcUaClient = opcUaClient;
  22. }
  23. private void FormConnectSelect_Load( object sender, EventArgs e )
  24. {
  25. }
  26. private OpcUaClient m_OpcUaClient;
  27. private void button1_Click( object sender, EventArgs e )
  28. {
  29. // 匿名登录
  30. m_OpcUaClient.UserIdentity = new UserIdentity( new AnonymousIdentityToken( ) );
  31. DialogResult = DialogResult.OK;
  32. return;
  33. }
  34. private void button2_Click( object sender, EventArgs e )
  35. {
  36. // 用户名密码登录
  37. m_OpcUaClient.UserIdentity = new UserIdentity( textBox1.Text, Encoding.UTF8.GetBytes( textBox2.Text ) );
  38. DialogResult = DialogResult.OK;
  39. return;
  40. }
  41. private async void button3_Click( object sender, EventArgs e )
  42. {
  43. // 证书登录
  44. try
  45. {
  46. // 如果需要生成自签名的证书,可以参考网址:http://www.hsltechnology.cn/Doc/HslCommunication?chapter=HslCommChapter6-5
  47. X509Certificate2 certificate = new X509Certificate2( textBox4.Text, textBox3.Text, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable );
  48. m_OpcUaClient.UserIdentity = new UserIdentity( certificate );
  49. FileInfo fileInfo = new FileInfo( textBox4.Text );
  50. m_OpcUaClient.AppConfig.SecurityConfiguration.ApplicationCertificate.StoreType = CertificateStoreType.Directory;
  51. m_OpcUaClient.AppConfig.SecurityConfiguration.ApplicationCertificate.StorePath = fileInfo.DirectoryName;
  52. m_OpcUaClient.AppConfig.SecurityConfiguration.ApplicationCertificate.SubjectName = null;
  53. m_OpcUaClient.AppConfig.SecurityConfiguration.TrustedPeerCertificates.StoreType = CertificateStoreType.Directory;
  54. m_OpcUaClient.AppConfig.SecurityConfiguration.TrustedPeerCertificates.StorePath = fileInfo.DirectoryName;
  55. m_OpcUaClient.AppConfig.SecurityConfiguration.TrustedIssuerCertificates.StoreType = CertificateStoreType.Directory;
  56. m_OpcUaClient.AppConfig.SecurityConfiguration.TrustedIssuerCertificates.StorePath = fileInfo.DirectoryName;
  57. // 创建一个应用实例对象,用于检查证书
  58. var application = new ApplicationInstance( m_OpcUaClient.AppConfig, null );
  59. // 检查应用实例对象的证书
  60. bool check = await application.CheckApplicationInstanceCertificatesAsync( false, 2048 );
  61. DialogResult = DialogResult.OK;
  62. return;
  63. }
  64. catch(Exception ex)
  65. {
  66. ClientUtils.HandleException( this.Text, ex );
  67. }
  68. }
  69. private void button4_Click( object sender, EventArgs e )
  70. {
  71. using (OpenFileDialog openFileDialog = new OpenFileDialog( ))
  72. {
  73. openFileDialog.Multiselect = false;
  74. if (openFileDialog.ShowDialog( ) == DialogResult.OK)
  75. {
  76. textBox4.Text = openFileDialog.FileName;
  77. }
  78. }
  79. }
  80. }
  81. }