mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-22 17:50:03 +08:00
Use secure connection in console sample
This commit is contained in:
@@ -19,11 +19,11 @@ namespace NetCoreConsoleClient
|
||||
public static async Task ConsoleSampleClient()
|
||||
{
|
||||
Console.WriteLine("1 - Create an Application Configuration.");
|
||||
|
||||
var config = new ApplicationConfiguration()
|
||||
{
|
||||
ApplicationName = "UA Sample Client",
|
||||
ApplicationType = ApplicationType.Client,
|
||||
ApplicationUri = "urn:localhost:OPCFoundation:SampleClient",
|
||||
SecurityConfiguration = new SecurityConfiguration
|
||||
{
|
||||
ApplicationCertificate = new CertificateIdentifier
|
||||
@@ -51,18 +51,22 @@ namespace NetCoreConsoleClient
|
||||
};
|
||||
await config.Validate(ApplicationType.Client);
|
||||
|
||||
if (config.SecurityConfiguration.AutoAcceptUntrustedCertificates)
|
||||
if (!config.SecurityConfiguration.AutoAcceptUntrustedCertificates)
|
||||
{
|
||||
config.CertificateValidator.CertificateValidation += new CertificateValidationEventHandler(CertificateValidator_CertificateValidation);
|
||||
}
|
||||
|
||||
Console.WriteLine("2 - Create a session with .Net UA Sample server.");
|
||||
Console.WriteLine("2 - Discover endpoints of .Net UA Sample server.");
|
||||
string endpointURL = "opc.tcp://" + Dns.GetHostName() + ":51210/UA/SampleServer";
|
||||
ConfiguredEndpoint endpoint = new ConfiguredEndpoint(null, new EndpointDescription(endpointURL));
|
||||
var endpointCollection = DiscoverEndpoints(config, new Uri(endpointURL), 10);
|
||||
|
||||
var session = await Session.Create(config, endpoint, true, "", 60000, null, null);
|
||||
Console.WriteLine("3 - Create a secure session with .Net UA Sample server.");
|
||||
var secureEndpoint = SelectSecureEndpoint(endpointCollection);
|
||||
var endpointConfiguration = EndpointConfiguration.Create(config);
|
||||
var endpoint = new ConfiguredEndpoint(secureEndpoint.Server, endpointConfiguration);
|
||||
var session = await Session.Create(config, endpoint, true, ".NetCore Console Client", 60000, null, null);
|
||||
|
||||
Console.WriteLine("3 - Browse the server namespace.");
|
||||
Console.WriteLine("4 - Browse the server namespace.");
|
||||
ReferenceDescriptionCollection references;
|
||||
Byte[] continuationPoint;
|
||||
|
||||
@@ -104,19 +108,19 @@ namespace NetCoreConsoleClient
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("4 - Create a subscription with publishing interval of 1 second.");
|
||||
Console.WriteLine("5 - Create a subscription with publishing interval of 1 second.");
|
||||
var subscription = new Subscription(session.DefaultSubscription) { PublishingInterval = 1000 };
|
||||
|
||||
Console.WriteLine("5 - Add a list of items (server current time and status) to the subscription.");
|
||||
Console.WriteLine("6 - Add a list of items (server current time and status) to the subscription.");
|
||||
var list = new List<MonitoredItem> { new MonitoredItem(subscription.DefaultItem) { DisplayName = "ServerStatusCurrentTime", StartNodeId = "i=2258" } };
|
||||
list.ForEach(i => i.Notification += OnNotification);
|
||||
subscription.AddItems(list);
|
||||
|
||||
Console.WriteLine("6 - Add the subscription to the session.");
|
||||
Console.WriteLine("7 - Add the subscription to the session.");
|
||||
session.AddSubscription(subscription);
|
||||
subscription.Create();
|
||||
|
||||
Console.WriteLine("Press any key to exit...");
|
||||
Console.WriteLine("8 - Running...Press any key to exit...");
|
||||
Console.ReadKey(true);
|
||||
|
||||
}
|
||||
@@ -134,5 +138,58 @@ namespace NetCoreConsoleClient
|
||||
e.Accept = (e.Error.StatusCode == StatusCodes.BadCertificateUntrusted);
|
||||
}
|
||||
|
||||
private static EndpointDescriptionCollection DiscoverEndpoints(ApplicationConfiguration config, Uri discoveryUrl, int timeout)
|
||||
{
|
||||
// use a short timeout.
|
||||
EndpointConfiguration configuration = EndpointConfiguration.Create(config);
|
||||
configuration.OperationTimeout = timeout;
|
||||
|
||||
using (DiscoveryClient client = DiscoveryClient.Create(
|
||||
discoveryUrl,
|
||||
EndpointConfiguration.Create(config)))
|
||||
{
|
||||
try
|
||||
{
|
||||
EndpointDescriptionCollection endpoints = client.GetEndpoints(null);
|
||||
ReplaceLocalHostWithRemoteHost(endpoints, discoveryUrl);
|
||||
return endpoints;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Utils.Trace("Could not fetch endpoints from url: {0}. Reason={1}", discoveryUrl, e.Message);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static EndpointDescription SelectSecureEndpoint(EndpointDescriptionCollection endpointCollection)
|
||||
{
|
||||
EndpointDescription bestEndpoint = null;
|
||||
foreach (EndpointDescription endpoint in endpointCollection)
|
||||
{
|
||||
if (endpoint.TransportProfileUri == Profiles.UaTcpTransport)
|
||||
{
|
||||
if (bestEndpoint == null || endpoint.SecurityLevel > bestEndpoint.SecurityLevel)
|
||||
{
|
||||
bestEndpoint = endpoint;
|
||||
}
|
||||
}
|
||||
}
|
||||
return bestEndpoint;
|
||||
}
|
||||
|
||||
private static void ReplaceLocalHostWithRemoteHost(EndpointDescriptionCollection endpoints, Uri discoveryUrl)
|
||||
{
|
||||
foreach (EndpointDescription endpoint in endpoints)
|
||||
{
|
||||
endpoint.EndpointUrl = Utils.ReplaceLocalhost(endpoint.EndpointUrl, discoveryUrl.DnsSafeHost);
|
||||
StringCollection updatedDiscoveryUrls = new StringCollection();
|
||||
foreach (string url in endpoint.Server.DiscoveryUrls)
|
||||
{
|
||||
updatedDiscoveryUrls.Add(Utils.ReplaceLocalhost(url, discoveryUrl.DnsSafeHost));
|
||||
}
|
||||
endpoint.Server.DiscoveryUrls = updatedDiscoveryUrls;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user