我正在嘗試使用 WCF Web 服務參考來使用 SOAP Web 服務。
我已經能夠使用 System.Web.Servicees Web 服務參考在 .NET 4.8 框架專案中成功使用 SOAP Web 服務。但是,我需要在 .NET Core 專案中使用 Web 服務。從 WSDL 生成的 WCF 類不同于 .NET 框架 Web 服務。看來您現在必須使用生成的 WebServiceClient 與 Web 服務進行互動。
我相信 Web 服務需要基本身份驗證,因為我能夠在 .NET 框架專案中使用基本身份驗證進行身份驗證。
這是我在嘗試執行 Web 服務的方法之一時收到的錯誤訊息。
System.ServiceModel.Security.MessageSecurityException
HResult=0x80131500
Message=The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was ''.
Source=System.Private.ServiceModel
這是我的代碼,它實體化客戶端并呼叫該方法
var callContext = new CAdxCallContext();
callContext.codeLang = "ENG";
callContext.poolAlias = "BGRTEST";
callContext.requestConfig = "adxwss.trace.on=on&adxwss.trace.size=16384&adonix.trace.on=on&adonix.trace.level=3&adonix.trace.size=8";
var proxy = new CAdxWebServiceXmlCCClient();
proxy.ClientCredentials.UserName.UserName = "username";
proxy.ClientCredentials.UserName.Password = "password";
string _InputXml = "<PARAM>"
"<GRP ID= \"GRP1\">"
"<FLD NAME = \"ITMREF\">" 100001 "</FLD>"
"</GRP>"
"</PARAM>";
try
{
var response = proxy.run(callContext, "BGR_SIEPRO", _InputXml);
}
finally
{
proxy.Close();
}
我的 WCF 服務連接: WCF Connected Service Screenshot
自動生成的 WCF WebServiceClient:https ://github.com/abiddle-bgr/Test/blob/main/CAdxWebServiceXmlCCClient.cs
uj5u.com熱心網友回復:
您是否設定了安全傳輸模式?與此類似:WCF 客戶端中的基本身份驗證。
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
在代碼中,設定代理類允許模擬
proxy.ClientCredentials.Windows.AllowedImpersonationLevel =
System.Security.Principal.TokenImpersonationLevel.Impersonation;
你可以看看這個帖子。
uj5u.com熱心網友回復:
我最終不得不通過創建自定義端點并將其作為自定義端點行為添加到代理來手動注入標頭。
var proxy = new CAdxWebServiceXmlCCClient();
proxy.Endpoint.EndpointBehaviors.Add(new CustomEndpoint());
public class ClientMessageInspector : IClientMessageInspector
{
public void AfterReceiveReply(ref Message reply, object correlationState)
{
// Nothing Here
Console.Write(reply.ToString());
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic "
Convert.ToBase64String(Encoding.ASCII.GetBytes("USERNAME" ":"
"PASSWORD"));
request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestProperty);
return null;
}
}
public class CustomEndpoint : IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
// Nothing here
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.ClientMessageInspectors.Add(new ClientMessageInspector());
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
// Nothing here
}
public void Validate(ServiceEndpoint endpoint)
{
// Nothing here
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/448449.html