我正在尝试使用 Opc.Ua Foundation 库通过我的 C# 应用程序连接到服务器,但我得到:
System.AggregateException: '发生一个或多个错误。 (端点不支持提供的用户身份类型。)'
服务器安全设置为无。
这是我的代码:
// Generate a client application
ApplicationInstance application = new ApplicationInstance();
application.ApplicationType = ApplicationType.Client;
// Load the configuration file
application.LoadApplicationConfiguration(@"path_to_file.xml", false).Wait();
ApplicationConfiguration m_configuration = application.ApplicationConfiguration;
// Get the endpoint
EndpointDescription endpointDescription = new EndpointDescription("opc.tcp://192.168.0.213:4840/");
EndpointConfiguration endpointConfiguration = EndpointConfiguration.Create(m_configuration);
ConfiguredEndpoint endpoint = new ConfiguredEndpoint(null, endpointDescription, endpointConfiguration);
// Create the session
_uaSession = Session.Create(
m_configuration, endpoint, false, false, m_configuration.ApplicationName, (uint)m_configuration.ClientConfiguration.DefaultSessionTimeout, new UserIdentity(), null)
.Result;
您需要使用
获得
endpointDescription
CoreClientUtils.SelectEndpoint(endpointurl, security);
安全(使用证书)是一个布尔值
我为你做了一个示例代码:
var config = new ApplicationConfiguration()
{
ApplicationName = "MyClient",
ApplicationUri = Utils.Format(@"urn:{0}:MyClient", System.Net.Dns.GetHostName()),
ApplicationType = ApplicationType.Client,
SecurityConfiguration = new SecurityConfiguration
{
ApplicationCertificate = new CertificateIdentifier { StoreType = @"Directory", StorePath = @"%CommonApplicationData%\OPC Foundation\CertificateStores\MachineDefault", SubjectName = "MyClientSubjectName" },
TrustedIssuerCertificates = new CertificateTrustList { StoreType = @"Directory", StorePath = @"%CommonApplicationData%\OPC Foundation\CertificateStores\UA Certificate Authorities" },
TrustedPeerCertificates = new CertificateTrustList { StoreType = @"Directory", StorePath = @"%CommonApplicationData%\OPC Foundation\CertificateStores\UA Applications" },
RejectedCertificateStore = new CertificateTrustList { StoreType = @"Directory", StorePath = @"%CommonApplicationData%\OPC Foundation\CertificateStores\RejectedCertificates" },
AutoAcceptUntrustedCertificates = true
},
TransportConfigurations = new TransportConfigurationCollection(),
TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 },
TraceConfiguration = new TraceConfiguration()
};
config.Validate(ApplicationType.Client).GetAwaiter().GetResult();
if (config.SecurityConfiguration.AutoAcceptUntrustedCertificates)
{
config.CertificateValidator.CertificateValidation += (s, e) => { e.Accept = (e.Error.StatusCode == StatusCodes.BadCertificateUntrusted); };
}
var application = new ApplicationInstance
{
ApplicationName = "MyClient",
ApplicationType = ApplicationType.Client,
ApplicationConfiguration = config
};
application.CheckApplicationInstanceCertificate(false, 2048).GetAwaiter().GetResult();
EndpointDescription endpointDescription = CoreClientUtils.SelectEndpoint("opc.tcp://192.168.54.200:4840/", false);
EndpointConfiguration endpointConfiguration = EndpointConfiguration.Create(config);
ConfiguredEndpoint endpoint = new ConfiguredEndpoint(null, endpointDescription, endpointConfiguration);
UserIdentity user = new UserIdentity();
bool useCredentials = false;
if (useCredentials)
{
user = new UserIdentity("admin", "admin");
}
// Sets whether or not the discovery endpoint is used to update the endpoint description before connecting.
bool updateBeforeConnect = false;
// Sets whether or not the domain in the certificate must match the endpoint used
bool checkDomain = false;
// The name to assign to the session
string sessionName = config.ApplicationName;
// The session's timeout interval
uint sessionTimeout = 60000;
// List of preferred locales
List<string> preferredLocales = null;
// Create the session
Session session = Session.Create(
config,
endpoint,
updateBeforeConnect,
checkDomain,
sessionName,
sessionTimeout,
user,
preferredLocales
).Result;
if (session != null && session.Connected)
{
Console.WriteLine("connected");
}
}
你可能需要改变一些东西,但这是最基本的。您不在此处使用证书(如果将安全性更改为 true,则可以使用)。在我的个人资料中有一些问题可以回答更多关于证书的问题。 还要确保你可以访问你的 OPC ua 服务器(我猜你的是西门子,因为端口)是开放的来宾身份验证 en 没有安全(证书)你可以在 tia 门户 - > plc 设置 - > opc ua 服务器设置中更改它
编辑:您也可以使用 xml 文件(就像您现在正在做的那样)。如果你这样做,你不需要我上面的代码
EndpointDescription endpointDescription