在连接Xamarin表单时,在WCF中禁止使用如何解决DTD



我试图在Xamarine Forms中连接我的WCF服务,但在运行时我得到此错误

出于安全原因,本XML文档禁止使用DTD。要启用DTD处理,请将XmlReaderSettings上的DtdProcessing属性设置为Parse,并将设置传递给XmlReader。创建方法。

LoginWCFService.svc.cs

public class LoginWCFService : ILoginWCFService
{   
public string LoginUserDetails(UserDetails userInfo)
{
string result = string.Empty;
bool res = false;
if (userInfo.uName!="" && userInfo.pWord != "" || userInfo.uName != null && userInfo.pWord != null)
{   
result = "Login Successfull...";                
}
else
{
res = false;
result = "Empty username or password";
}
return result.ToString();
}
}

ILoginWCFService.cs

[ServiceContract]
public interface ILoginWCFService
{
[OperationContract]        
string LoginUserDetails(UserDetails UserInfo);
}
public class UserDetails
{
string UserName = string.Empty;
string Password = string.Empty;
[DataMember]
public string uName
{
get { return UserName; }
set { UserName = value; }
}
[DataMember]
public string pWord
{
get { return Password; }
set { Password = value; }
}
}

web . config

<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2"/>
</system.web>
<system.serviceModel>
<behaviors>     
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>        
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ILoginWCFService" />        
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://wcfapi.local/LoginWCFService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ILoginWCFService"
contract="WcfService_Omss.ILoginWCFService" name="BasicHttpBinding_ILoginWCFService" />
</client>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>

以Xamarine形式使用WCF

LoginPage.xaml.cs

WCFServiceReference.LoginWCFServiceClient client = new LoginWCFServiceClient();
client.Open();
UserDetails user = new UserDetails();
user.uName = Uname.Text.ToString();
user.pWord = Pass.Text.ToString();
var res =  client.LoginUserDetails(user);  //Getting error here
if (res == "Login Successfull...")
await DisplayAlert("Message", "Login Successfull...", "Cancel");
else
await DisplayAlert("Message", "Login failed...", "Cancel");
client.Close();

谁能告诉我在哪里我必须做改变来解决这个错误?

此错误与wcf服务无关。
如消息所示,将XmlReaderSettings上的DtdProcessing属性设置为Parse,并将设置传递给XmlReader.Create方法。
可以参考下面的代码
https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlreadersettings.dtdprocessing?view=net-6.0

using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
public class Sample {
public static void Main() {
// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;
settings.ValidationType = ValidationType.DTD;
settings.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
// Create the XmlReader object.
XmlReader reader = XmlReader.Create("itemDTD.xml", settings);
// Parse the file.
while (reader.Read());
}
// Display any validation errors.
private static void ValidationCallBack(object sender, ValidationEventArgs e) {
Console.WriteLine("Validation Error: {0}", e.Message);
}
}

相关内容

最新更新