如何解决.net core 3.1 无法对远程服务进行身份验证
问题来了。我正在将我的出站服务调用从 Framework 4.7 转换为 Core 3.1。一切都在 Framework 中完美运行,但无法在 Core 3.1 中进行身份验证。我通过在连接服务中添加远程服务来使用它。 WSDL 在 Core 中构建了所有相同的类等。在有人问之前,我在 Framework 和 Core 中使用相同的用户名和密码,并且它在 Framework 中工作。我基本上是将代码直接从 Framework 项目复制到 Core 项目,并进行了细微的调整以使其能够编译。
我尝试了几种变体,但都没有成功。我希望所有设置都在 ConnectedService.json 中,但我对如何执行此操作的搜索没有返回任何有用的结果。
任何帮助将不胜感激,因为我在过去 3 天里一直在转动我的轮子。
提前致谢
web.config (Works) 中的框架设置:
<client>
<endpoint address="xxxxx.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPatientService" contract="PatientService.IPatientService" name="BasicHttpBinding_IPatientService" />
</Client>
<binding name="BasicHttpBinding_IPatientService" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false" bypassproxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNaMetableCharCount="16384" />
<security mode="Transport">
<transport clientCredentialType="Basic" proxyCredentialType="None" realm="Basic Realm" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
框架代码(作品):
public PatientServiceHelper()
{
// Force TLS1.2
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
// Authenticate
client.ClientCredentials.UserName.UserName = "MyUserName";
client.ClientCredentials.UserName.Password = "MyPassword";
}
public bool TestPatientServiceModule()
{
PatientService.PatientPhoneNumberSearchRequest patientPhoneNumberSearchRequest = new PatientService.PatientPhoneNumberSearchRequest();
patientPhoneNumberSearchRequest.PatientID= 1;
PatientService.PatientPhonesearchSortParameter[] patientPhonesearchSortParameter = new PatientPhonesearchSortParameter[1];
patientPhonesearchSortParameter[0] = new PatientPhonesearchSortParameter();
patientPhonesearchSortParameter[0].sortOrder = SortOrder.Ascending;
PatientService.DataSearchServiceResponseUsingPatientPhoneNumberSearchResponse response = client.PatientPhoneNumberSearch(patientPhoneNumberSearchRequest,patientPhonesearchSortParameter,1,1);
return response.Success;
}
ConnectedService.json 中的核心设置(失败):
{
"ProviderId": "Microsoft.VisualStudio.ConnectedService.Wcf","Version": "15.0.40203.910","ExtendedData": {
"inputs": [
"xxxxx.svc"
],"collectionTypes": [
"System.Array","System.Collections.Generic.Dictionary`2"
],"namespaceMappings": [
"*,PatientService"
],"references": [
"System.ServiceModel,{System.ServiceModel.Primitives,4.4.4}","System.ServiceModel.Duplex,{System.ServiceModel.Duplex,"System.ServiceModel.Http,{System.ServiceModel.Http,"System.ServiceModel.NetTcp,{System.ServiceModel.NetTcp,"System.ServiceModel.Primitives,"System.ServiceModel.Security,{System.ServiceModel.Security,4.4.4}"
],"targetFramework": "netcoreapp3.1","typeReuseMode": "All"
}
}
核心代码尝试 1 初始化客户端(失败):
public PatientServiceHelper(string UserName,string Password)
{
// Force TLS1.2
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
// Authenticate
client.ClientCredentials.UserName.UserName = UserName;
client.ClientCredentials.UserName.Password = Password;
}
初始化客户端的核心代码尝试 2(失败):
public PatientServiceHelper(string UserName,string Password)
{
// Force TLS1.2
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ClientCredentials myCredentials = new ClientCredentials();
myCredentials.UserName.UserName = UserName;
myCredentials.UserName.Password = Password;
BasicHttpsBinding myBinding = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport);
myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
myBinding.CloseTimeout = TimeSpan.Parse("00:10:00");
myBinding.OpenTimeout = TimeSpan.Parse("00:10:00");
myBinding.ReceiveTimeout = TimeSpan.Parse("00:10:00");
myBinding.SendTimeout = TimeSpan.Parse("00:10:00");
myBinding.AllowCookies = false;
myBinding.BypassproxyOnLocal = false;
//hostnamecomparisonmode = StrongWildCard - Default Value
myBinding.MaxBufferSize = 2147483647;
myBinding.MaxBufferPoolSize = 524288;
myBinding.MaxReceivedMessageSize = 2147483647;
//MessageEncoding = Text - Default Value
myBinding.TextEncoding = Encoding.UTF8;
myBinding.TransferMode = TransferMode.Buffered;
myBinding.UseDefaultWebProxy = true;
myBinding.ReaderQuotas.MaxDepth = 32;
myBinding.ReaderQuotas.MaxStringContentLength = 8192;
myBinding.ReaderQuotas.MaxArrayLength = 16384;
myBinding.ReaderQuotas.MaxBytesPerRead = 4096;
myBinding.ReaderQuotas.MaxNaMetableCharCount = 16384;
EndpointAddress myEndpoint = new EndpointAddress("xxxxx.svc");
ChannelFactory<IPatientService> myChannel = new ChannelFactory<IPatientService>(myBinding,myEndpoint);
myChannel.Endpoint.EndpointBehaviors.Clear();
myChannel.Endpoint.EndpointBehaviors.Add(myCredentials);
IPatientService myClient = myChannel.CreateChannel();
client = myClient;
}
命中服务的代码(工作但由于身份验证问题而失败):
public bool TestPatientServiceModule()
{
PatientService.PatientPhoneNumberSearchRequest patientPhoneNumberSearchRequest = new PatientService.PatientPhoneNumberSearchRequest();
patientPhoneNumberSearchRequest.PatientID = 1;
PatientService.PatientPhonesearchSortParameter[] patientPhonesearchSortParameter = new PatientPhonesearchSortParameter[1];
patientPhonesearchSortParameter[0] = new PatientPhonesearchSortParameter();
patientPhonesearchSortParameter[0].sortOrder = SortOrder.Ascending;
var results = client.PatientPhoneNumberSearchAsync(patientPhoneNumberSearchRequest,1);
results.Wait();
return results.Result.Success;
}
初始化尝试 1 中的身份验证错误:
The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic Realm'.
初始化尝试 2 中的身份验证错误:
The HTTP request is unauthorized with client authentication scheme 'Basic'. The authentication header received from the server was 'Basic Realm'.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。