WCF通过XAMARIN - System.Net.WebException:请求失败,HTTP状态为403:Forbidden

时间:2017-10-27 03:17:33

标签: android web-services wcf xamarin.android web-reference

如何解决禁止访问问题?我通过网络参考访问我的服务,所以我的代码没有声明。是否有任何设置我必须包括在连接期间使证书有效?
我的Web服务是使用IIS绑定以及防火墙中的入站和出站设置的。为了创建证书,我遵循了this link for creating temporary client certificate

Web Reference

这是我的webconfig

<?xml version="1.0"?>
<configuration>

  <connectionStrings>
    <add name="constring" providerName="System.Data.SqlClient" connectionString="Data Source = ip;Initial Catalog = db; User ID = user; Password = pass"/>
  </connectionStrings>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>

  <system.web>
    <compilation debug="true" targetFramework="4.5" />
  </system.web>
  <system.serviceModel>
    <client>
      <endpoint behaviorConfiguration="ClientCertificateBehavior" 
        binding="basicHttpBinding"
        bindingConfiguration="SecureHttpsBinding" contract="IMetadataExchange" 
        name="https" />
    </client>
    <bindings> 
      <basicHttpBinding>
        <binding maxReceivedMessageSize="10485760" name="SecureHttpsBinding">
          <readerQuotas maxStringContentLength="10485760"></readerQuotas>
            <security mode="Transport">
              <transport clientCredentialType="Certificate"></transport>
            </security>
        </binding>
      </basicHttpBinding>
    </bindings>

    <services>
      <service name="RBOSService.AccountService" behaviorConfiguration="ServiceBehavior">
        <endpoint address="" contract="RBOSService.IAccountService" binding="basicHttpBinding" bindingConfiguration="SecureHttpsBinding"/>
      </service>
      <service name="RBOSService.OrderService" behaviorConfiguration="ServiceBehavior">
        <endpoint address="" contract="RBOSService.IOrderService" binding="basicHttpBinding" bindingConfiguration="SecureHttpsBinding"/>
      </service>
    </services>

    <behaviors>
      <endpointBehaviors>
        <behavior name="ClientCertificateBehavior">
          <clientCredentials>
            <clientCertificate findValue="CN=tempClientcertCN" 
              storeLocation="LocalMachine"
              storeName="My" 
              x509FindType="FindBySubjectDistinguishedName" />
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>    
</configuration>

我还在OnCreate()

中添加了trustIssue的代码
System.Net.ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;

并且在请求webService之后,我得到了关于禁止的例外。

userAccount = requestAccessService.sendRequestAccess(userRequest, ref response);

这是完整的异常消息

{System.Net.WebException: The request failed with HTTP status 403: Forbidden
  at System.Web.Services.Protocols.SoapHttpClientProtocol.ReceiveResponse (System.Net.WebResponse response, System.Web.Services.Protocols.SoapClientMessage message, System.Web.Services.Protocols.SoapExtension[] extensions) [0x00054] in <6f1079230fce4308ba6b44c62385411f>:0 
  at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke (System.String method_name, System.Object[] parameters) [0x000ad] in <6f1079230fce4308ba6b44c62385411f>:0 
  at BusinessLogic.RequestAccessWebService.RequestAccessService.sendRequestAccess (BusinessLogic.RequestAccessWebService.UserRequestBO userRequest, System.String& response) [0x00001] in D:\files\jeys\H2 software\GitLab\RBOS_Release_1\RBOS Mobile\RBOS 2.0.4 - base - Merged 0620\BusinessLogic\Web References\RequestAccessWebService\Reference.cs:123 
  at (wrapper remoting-invoke-with-check) BusinessLogic.RequestAccessWebService.RequestAccessService:sendRequestAccess (BusinessLogic.RequestAccessWebService.UserRequestBO,string&)
  at RBOS_2._0._1.RequestAccess.backgroundService (System.Object sender, System.ComponentModel.DoWorkEventArgs ev) [0x00131] in D:\files\jeys\H2 software\GitLab\RBOS_Release_1\RBOS Mobile\RBOS 2.0.4 - base - Merged 0620\RBOS 2.0.1\Activities\RequestAccess.cs:184 }

如果有任何问题,请随时发表评论。谢谢你的帮助。

0 个答案:

没有答案