Cross Domain Policy

我是研究僧i 提交于 2019-12-20 06:48:19

问题


I am working with a Silvelright App that consumes a WCF service, I have placed a crossdomain and clientaccesspolicy xml's in the wwwroot of the IIS as well as in the application folder!

yet when the client communicates with the service, it throws an error saying;

An error occurred while trying to make a request to URI ‘http://localhost:1528/MyService.svc’. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a ……

Please help! Thanks


回答1:


The clientaccesspolicy.xml needs to be on the same port as your service. It needs to be located at http://localhost:1528/clientaccesspolicy.xml

If you are self hosting the WCF service then you need to host the clientaccesspolicy.xml from within your WCF service. The easiest way I've found to do this is to add a separate service contract that provides an HTTP GET of the clientaccesspolicy.xml.

[ServiceContract()]
public class PolicyRetriever
{
    [OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
    public Stream GetSilverlightPolicy()
    {
        string result = @"<?xml version=""1.0"" encoding=""utf-8""?>
        <access-policy>
            <cross-domain-access>
                <policy>
                    <allow-from http-request-headers=""*"">
                        <domain uri=""*""/>
                    </allow-from>
                    <grant-to>
                        <resource path=""/"" include-subpaths=""true""/>
                    </grant-to>
                </policy>
            </cross-domain-access>
        </access-policy>";

        if (System.ServiceModel.Web.WebOperationContext.Current != null)
        {
            System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
        }

        return new MemoryStream(Encoding.UTF8.GetBytes(result));
    }
}


来源:https://stackoverflow.com/questions/2848608/cross-domain-policy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!