private void CertificateImpersonateBTN_Click(object sender, EventArgs e) { if (m_session == null) { return; } try { // load the certficate. X509Certificate2 certificate = new X509Certificate2( CertificateTB.Text, CertificatePasswordTB.Text, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable); // want to get error text for this call. m_session.ReturnDiagnostics = DiagnosticsMasks.All; UserIdentity identity = new UserIdentity(certificate); string[] preferredLocales = PreferredLocalesTB.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); m_session.UpdateSession(identity, preferredLocales); MessageBox.Show("User identity changed.", "Impersonate User", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception exception) { ClientUtils.HandleException(this.Text, exception); } finally { m_session.ReturnDiagnostics = DiagnosticsMasks.None; } }
private void OkBTN_Click(object sender, EventArgs e) { try { #region Task #D3 - Change Locale and User Identity UserIdentity identity = null; // use the anonymous identity of the user name is not provided. if (String.IsNullOrEmpty(UserNameTB.Text)) { identity = new UserIdentity(); } // could add check for domain name in user name and use a kerberos token instead. else { identity = new UserIdentity(UserNameTB.Text, PasswordTB.Text); } // can specify multiple locales but just use one here to keep the UI simple. StringCollection preferredLocales = new StringCollection(); preferredLocales.Add(LocaleCB.SelectedItem as string); // override the default diagnostics to get error messages. DiagnosticsMasks returnDiagnostics = m_session.ReturnDiagnostics; try { // update the session. m_session.ReturnDiagnostics = DiagnosticsMasks.ServiceSymbolicIdAndText; m_session.UpdateSession(identity, preferredLocales); } finally { m_session.ReturnDiagnostics = returnDiagnostics; } #endregion DialogResult = DialogResult.OK; } catch (Exception exception) { ClientUtils.HandleException(this.Text, exception); } }
private void UserNameImpersonateBTN_Click(object sender, EventArgs e) { if (m_session == null) { return; } try { // want to get error text for this call. m_session.ReturnDiagnostics = DiagnosticsMasks.All; UserIdentity identity = new UserIdentity(UserNameTB.Text, PasswordTB.Text); string[] preferredLocales = PreferredLocalesTB.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); m_session.UpdateSession(identity, preferredLocales); MessageBox.Show("User identity changed.", "Impersonate User", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception exception) { ClientUtils.HandleException(this.Text, exception); } finally { m_session.ReturnDiagnostics = DiagnosticsMasks.None; } }
/// <summary> /// Creates a new session. /// </summary> private Session CreateSession(Guid clsid) { // lookup the cached configuration information. ConfiguredEndpoint endpoint = null; bool previouslyConnected = true; if (!m_verifiedEndpoints.TryGetValue(clsid, out endpoint)) { endpoint = LoadConfiguredEndpoint(clsid); if (endpoint != null) { Utils.Trace("Loaded endpoint with URL: {0}", endpoint.EndpointUrl); previouslyConnected = false; } } if (endpoint == null) { endpoint = m_endpointCache.Create(DefaultServerUrl); } // Initialize the client configuration. // Fetch the current configuration information by connecting to the server's discovery endpoint. // This method assumes that the discovery endpoint can be constructed by appending "/discovery" to the URL. if (endpoint.UpdateBeforeConnect && !previouslyConnected) { endpoint.UpdateFromServer(BindingFactory.Default); Utils.Trace("Updated endpoint from server: {0}", endpoint.EndpointUrl); } // Need to specify that the server is trusted by the client application. if (!previouslyConnected) { m_configuration.SecurityConfiguration.AddTrustedPeer(endpoint.Description.ServerCertificate); } // Set the session keep alive to 600 seconds. m_configuration.ClientConfiguration.DefaultSessionTimeout = 600000; ServiceMessageContext messageContext = m_configuration.CreateMessageContext(); // Initialize the channel which will be created with the server. ITransportChannel channel = SessionChannel.Create( m_configuration, endpoint.Description, endpoint.Configuration, m_configuration.SecurityConfiguration.ApplicationCertificate.Find(true), messageContext); // Wrap the channel with the session object. Session session = new Session(channel, m_configuration, endpoint, null); session.ReturnDiagnostics = DiagnosticsMasks.SymbolicId; // The user login credentials must be provided when opening a session. IUserIdentity identity = null; if (endpoint.UserIdentity != null) { identity = new Opc.Ua.UserIdentity(endpoint.UserIdentity); } // Create the session. This actually connects to the server. session.Open("COM Client Session", identity); // need to fetch the references in order use the node cache. session.FetchTypeTree(ReferenceTypeIds.References); // save the updated information. if (!previouslyConnected) { try { SaveConfiguredEndpoint(clsid, endpoint); } catch (Exception e) { Utils.Trace(e, "Could not save SaveConfiguredEndpoint in registry."); } m_verifiedEndpoints.Add(clsid, endpoint); } return session; }