从Silverlight 4调用HTTPS-WCF服务时的SecurityError

时间:2010-11-03 11:30:59

标签: silverlight wcf https securityexception

我创建了一个简单的WCF服务,我想通过https访问它。 WCF服务使用UserNamePasswordValidator,customBinding和UserNameOverTransport作为身份验证模式。我在IIS7.5中运行它,在那里我创建了一个自签名服务器证书。

我尝试使用Silverlight 4应用程序连接到该服务。我在Silverlight 4应用程序中创建了一个服务引用,VS 2010会自动创建所需的代码(显然它能够连接到服务并获取信息)。

当我调用WCF时,我得到一个没有信息的SecurityException。

我有小提琴跑来看看发生了什么。

  

GET https://my.server:8099/clientaccesspolicy.xml 404 Not Found(text / html)
  GET https://my.server:8099/crossdomain.xml 200 OK(text / xml)

因此,crossdomain.xml的GET似乎是对服务器的最后一次调用。

crossdomain.xml如下所示:

<?xml version="1.0"?> 
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>  
  <allow-access-from domain="*" />  
  <allow-http-request-headers-from domain="*" headers="*" />
</cross-domain-policy>

当在客户端上调用base.EndInvoke(...)并且ExceptionMessage如下所示时发生异常:

{System.Security.SecurityException ---&gt; System.Security.SecurityException:Sicherheitsfehler    bei System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)    bei System.Net.Browser.BrowserHttpWebRequest。&lt;&gt; c_ DisplayClass5.b _4(Object sendState)    bei System.Net.Browser.AsyncHelper。&lt;&gt; c_ DisplayClass2.b _0(Object sendState)    --- Ende derinternenAusnahmestapelüberwachung---    bei System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,Object state)    bei System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)    bei System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)}

这是我的UserNamePasswordValidator。请注意,出于调试原因,它包含一个记录器。奇怪的是,记录器从不写任何东西,似乎甚至没有调用Validate函数。

namespace ServiceConfiguratorDataSource
{
  public class UserCredentialsValidator : UserNamePasswordValidator
  {
    public override void Validate(string userName, string password)
    {
      if (userName != "xyz" || password != "xyz")
      {
        logging.Logger.instance.StatusRoutine("LogOn Error: " + userName);
        throw new FaultException("Credentials are invalid");
      }
      else
      {
        logging.Logger.instance.StatusRoutine("LogOn Success: " + userName);
      }
    }
  }
}

这是我的WCF服务的Web.config:

<?xml version="1.0" encoding="utf-8"?>
  <configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.serviceModel>
      <services>
        <service name="ServiceConfiguratorDataSource.Service" behaviorConfiguration="ServiceConfiguratorDataSourceBehaviour">
          <endpoint address="" binding="customBinding" bindingConfiguration="ServiceConfiguratorCustomBinding" contract="ServiceConfiguratorDataSource.IService" />
        </service>
      </services>
      <bindings>
        <customBinding>
          <binding name="ServiceConfiguratorCustomBinding">
            <security authenticationMode="UserNameOverTransport"></security>
            <binaryMessageEncoding></binaryMessageEncoding>
            <httpsTransport/>
          </binding>
        </customBinding>
      </bindings>
      <behaviors>
        <serviceBehaviors>
          <behavior name="ServiceConfiguratorDataSourceBehaviour">
            <serviceMetadata httpsGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="true"/>
            <serviceCredentials>
              <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="ServiceConfiguratorDataSource.UserCredentialsValidator,ServiceConfiguratorDataSource" />
            </serviceCredentials>
          </behavior>
        </serviceBehaviors>
      </behaviors>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
    <system.webServer>
      <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
  </configuration>

这里是ServiceReferences.ClientConfig

<configuration>
  <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="CustomBinding_IService">
          <security authenticationMode="UserNameOverTransport" includeTimestamp="true">
            <secureConversationBootstrap />
          </security>
          <binaryMessageEncoding />
          <httpsTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
        </binding>
      </customBinding>
    </bindings>
    <client>
      <endpoint address="https://my.server:8099/ServiceConfiguratorDataSource/Service.svc" binding="customBinding" bindingConfiguration="CustomBinding_IService" contract="WCFDataProvider.IService" name="CustomBinding_IService" />
    </client>
  </system.serviceModel>
</configuration>

我的想法可能是导致问题的原因。

提前致谢,
弗兰克

2 个答案:

答案 0 :(得分:2)

clientaccesspolicy.xml是否允许https?
Here就是例子。

答案 1 :(得分:1)

正在运行的clientaccesspolicy.xml - &gt;来自Samvel Siradeghyan的Tipp

<?xml version="1.0" encoding="utf-8"?>
  <access-policy>
    <cross-domain-access>
      <policy>
        <allow-from http-request-headers="*">
          <domain uri="https://*"/>
          <domain uri="http://*"/>
        </allow-from>
        <grant-to>
          <resource path="/" include-subpaths="true"/>
        </grant-to>
      </policy>
    </cross-domain-access>
  </access-policy>