简体   繁体   English

How to Solve DTD is prohibited in this xml document in WCF while connecting with Xamarin.forms

[英]How to Solve DTD is prohibited in this xml document in WCF while connecting with Xamarin forms

I'm trying to connect my WCF service in Xamarine Forms, but in runtime I am getting this error我正在尝试在 Xamarine Forms 中连接我的 WCF 服务,但在运行时出现此错误

For security reasons DTD is prohibited in this XML document.出于安全原因,此 XML 文档中禁止使用 DTD。 To enable DTD processing set the DtdProcessing property on XmlReaderSettings to Parse and pass the settings into XmlReader.Create method.要启用 DTD 处理,请将 XmlReaderSettings 上的 DtdProcessing 属性设置为 Parse,并将设置传递给 XmlReader.Create 方法。

LoginWCFService.svc.cs登录WCFService.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 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 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>

Consuming WCF in Xamarine Form以 Xamarine 形式消费 WCF

LoginPage.xaml.cs登录页面.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();

Can anyone please tell me where I have to do changes to resolve this error?谁能告诉我必须在哪里进行更改才能解决此错误?

This error has nothing to do with the wcf service.此错误与 wcf 服务无关。
As the message says, set the DtdProcessing property on XmlReaderSettings to Parse and pass the settings to the XmlReader.Create method.如消息所述,将XmlReaderSettings上的 DtdProcessing 属性设置为Parse ,并将设置传递给XmlReader.Create方法。
You can refer to the code below.您可以参考下面的代码。
https://learn.microsoft.com/en-us/do.net/api/system.xml.xmlreadersettings.dtdprocessing?view.net-6.0 https://learn.microsoft.com/en-us/do.net/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);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 此XML文档中禁止DTD - DTD is prohibited in this XML document 此XML文档中禁止使用DTD-如何更改权限? - DTD is prohibited in this XML document — how to change permissions? DTD 在 xml 文档异常中被禁止 - DTD prohibited in xml document exception EWS 托管 API 出现错误“此 xml 文档中禁止 DTD dtd” - EWS Managed API Getting Error "DTD dtd is prohibited in this xml document" 如何在xamarin.forms中使用WCF Web服务时解决targetinvokationexception? - How to solve targetinvokationexception while consuming wcf webservices in xamarin.forms? 从Outlook中读取电子邮件时,获取内部异常“此XML文档交换中禁止使用dtd” - Getting inner exception “dtd is prohibited in this xml document exchange” while reading emails from outlook 迷你吧。 出于安全原因,此 XML 文档中禁止使用 DTD - Minio. For security reasons DTD is prohibited in this XML document 异常:'出于安全原因,在将 XML 转换为 XSL-FO 时,此 XML 文档中禁止 DTD - Exception: 'For security reasons DTD is prohibited in this XML document when converting XML to XSL-FO 如何在XML文档中包含DTD - How to include DTD in a XML document XML 文档 C# 中禁止的 DTD DtdProcessing 设置为 DtdProcessing.Parse - DTD Prohibited in XML Document C# Exception with DtdProcessing set to DtdProcessing.Parse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM