gpt4 book ai didi

c# - 从 C# .NET 应用程序连接到 SAP Web 服务

转载 作者:太空狗 更新时间:2023-10-29 20:37:51 24 4
gpt4 key购买 nike

我编写了一个 Windows 应用程序来测试与客户端 SAP Web 服务的连接。 Web 服务调用需要 X509 证书安全。

阅读互联网上的各种文章后,我想出了三种将 X509 证书附加到 Web 服务调用的方法。不幸的是,所有这些尝试都会返回“401 Unauthorized Access”。但是,我可以通过 IE 中的 URL 连接到 Web 服务。

有人对我可能做错了什么有任何建议吗?我正在使用 WSE 3.0,我用来附加证书的三种方法如下:-

证书

X509Certificate2 oCert = GetSecurityCertificate(oCertificate);  
svc.ClientCertificates.Add(oCert);

token

X509SecurityToken oToken = GetSecurityToken(oCertificate);
svc.RequestSoapContext.Security.Tokens.Add(oToken);

政策

SAPX509Assertion sapX509Assertion = new SAPX509Assertion(oCertificate, oStoreLocation, oStoreName, oFindType);  
svc.SetPolicy(sapX509Assertion.Policy());

GetSecurityToken() 和 GetSecuirtyCertificate 都搜索证书库。 SAPX509Assertion 这样做:-

public SAPX509Assertion(String certSubject, StoreLocation oStoreLocation, StoreName oStoreName, X509FindType oFindType)  
{
ClientX509TokenProvider = new X509TokenProvider(oStoreLocation,
oStoreName, certSubject, oFindType);
ServiceX509TokenProvider = new X509TokenProvider(oStoreLocation,
oStoreName, certSubject, oFindType);

Protection.Request.EncryptBody = false;
Protection.Response.EncryptBody = false;
}

更新好的,我现在有一个 WCF 调用。我无法使用 Eugarps 显示的 BasicHttpBinding 方法,因为它提示我正在连接到 https 地址并期望 http...这是有道理的。我现在拥有的代码是:-

var binding = new WSHttpBinding();
binding.MaxReceivedMessageSize = int.MaxValue;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
binding.Security.Mode = SecurityMode.Transport;

WCFConnection.CreateAbsenceWSlow.ZWSDHTM_GB_AMS_CREATEABS_lowClient client;
CreateAbsenceWSlow.ZfhhrGbbapiZgeeamsCreateabsResponse response;
CreateAbsenceWSlow.ZfhhrGbbapiZgeeamsCreateabs data;
//Assign address
var address = new EndpointAddress(sUrl);

//Create service client
client = new CreateAbsenceWSlow.ZWSDHTM_GB_AMS_CREATEABS_lowClient(binding, address);

//Assign credentials
client.ClientCredentials.UserName.UserName = sUserName;
client.ClientCredentials.UserName.Password = sPassword;

response = new CreateAbsenceWSlow.ZfhhrGbbapiZgeeamsCreateabsResponse();
data = new WCFConnection.CreateAbsenceWSlow.ZfhhrGbbapiZgeeamsCreateabs();

response = client.ZfhhrGbbapiZgeeamsCreateabs(data);

它仍然无法连接到 SAP 网络服务。我收到的错误是“HTTP 请求未经客户端身份验证方案‘协商’授权”。我也尝试过使用

binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

返回了类似的错误。

有人对我哪里出错有任何进一步的建议或想法吗?

最佳答案

现在,这一切都来 self 自己的经验,所以其中一些可能是错误的,但这是我对流程的理解(我没有收到任何文档,而且我的公司在我开始之前没有调用 SAP 的经验)。

SAP WS 调用仅受 WCF BasicHttpBinding 支持,据我所知,仅使用纯文本凭据。这意味着如果您需要使您的通信保密(在 Intranet 外部,或 Intranet 内的敏感数据),您将希望使用 IPSec 或 HTTPS。我们的 SAP 服务器没有配置 HTTPS,但我们使用 VPN 和 IPSec 进行外部通信。需要注意的重要一点是,默认情况下,SAP GUI 也不会将通信设为私有(private)。在这种情况下,使用下面详述的方法与在 GUI 7.1 中查找敏感数据的大厅下的业务用户一样安全。以下是我如何在内部连接到我们的 SAP 服务器:

        //Create binding
//Note, this is not secure but it's not up to us to decide. This should only ever be run within
//the VPN or Intranet where IPSec is active. If SAP is ever directly from outside the network,
//credentials and messages will not be private.
var binding = new BasicHttpBinding();
binding.MaxReceivedMessageSize = int.MaxValue;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;

//Assign address
var address = new EndpointAddress(Host);

//Create service client
var client = new SAP_RFC_READ_TABLE.RFC_READ_TABLEPortTypeClient(binding, address);

//Assign credentials
client.ClientCredentials.UserName.UserName = User;
client.ClientCredentials.UserName.Password = Password;

据我所知,不支持消息级安全性,并且不支持 basicHttpBinding (SOAP 1.1) 以外的绑定(bind)。

正如我所说,这完全来自经验而非培训,所以如果有人可以通过评论添加一些内容,请这样做。

关于c# - 从 C# .NET 应用程序连接到 SAP Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3666436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com