.net 将WCF客户端升级到CoreWCF会破坏身份验证

9gm1akwq  于 2023-08-08  发布在  .NET
关注(0)|答案(1)|浏览(142)

我有一个来自.NET Framework 4.8的WCF客户端,我需要将其移植到.NET Core,因为使用旧的客户端在升级基础项目后会给出PlatformNotSupportedException
客户端是从WSDL文件生成的,在VS中将其添加为Connected Service可以很好地更新Reference.cs客户端,主要更改是GeneratedCodeAttribute被设置为"Microsoft.Tools.ServiceModel.Svcutil", "2.1.0"而不是"System.ServiceModel", "4.0.0.0"(在将dotnet-svcutil安装到项目中之后)。它还安装了这两个包,我相信在较新的.NET版本中是contains the CoreWCF client functionality

  • System.ServiceModel.Http(6.0.0)
  • System.ServiceModel.Security(6.0.0)

我有一个集成,它正在创建一个生成的客户端,然后调用一个端点:

public string GetArticle(string articleNumber, string goal)
{
    using var client = CreateArtikelClient();
    return client.getArticle(articleNumber, goal);
}

private ArtikelPortTypeClient CreateArtikelClient()
{
    var client = new ArtikelPortTypeClient(ArtikelPortTypeClient.EndpointConfiguration.ArtikelPort);
    client.ClientCredentials.UserName.UserName = _integrationConfiguration.UserName;
    client.ClientCredentials.UserName.Password = _integrationConfiguration.Password;
    return client;
}

字符串
客户端创建得很好,但是调用getArticle会导致以下异常:

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 'Basic realm="Login please"'.
  Source=System.ServiceModel.Http
  StackTrace:
   at System.ServiceModel.Channels.HttpResponseMessageHelper.ValidateAuthentication()
   at System.ServiceModel.Channels.HttpResponseMessageHelper.<ParseIncomingResponse>d__7.MoveNext()
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpClientRequestChannel.HttpClientChannelAsyncRequest.<ReceiveReplyAsync>d__18.MoveNext()
   at System.ServiceModel.Channels.RequestChannel.<RequestAsync>d__33.MoveNext()
   at System.ServiceModel.Channels.RequestChannel.<RequestAsyncInternal>d__32.MoveNext()
   at System.Runtime.TaskHelpers.WaitForCompletionNoSpin[TResult](Task`1 task)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(MethodCall methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(MethodInfo targetMethod, Object[] args)
   at generatedProxy_1.getArticle(getArticle3Request )
   // my code calling the generated client


因此,以这种方式设置用户名和密码似乎不再足够,尽管它在.NET Framework中工作正常。我已经看到其他关于CoreWCF的帖子,其中凭据以这种方式设置,所以我不知道我错过了什么?

ecfdbz9o

ecfdbz9o1#

事实证明,客户端的构造需要略有不同,直接传入端点URL,并且需要首先创建绑定。

private ArtikelPortTypeClient CreateArtikelClient()
{
    var binding = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport);
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

    var client = new ArtikelPortTypeClient(binding, new EndpointAddress(_integrationConfiguration.EndpointAddress));
    client.ClientCredentials.UserName.UserName = _integrationConfiguration.UserName;
    client.ClientCredentials.UserName.Password = _integrationConfiguration.Password;
    return client;
}

字符串

相关问题