【问题标题】:Silverlight HttpWebRequest SecurityExceptionSilverlight HttpWebRequest 安全异常
【发布时间】:2012-01-08 04:21:24
【问题描述】:

问题

我有一个在远程服务器上运行的宁静 Web 服务。我已经制作了一个使用它的 WP7 应用程序,所以我知道它可以工作。我正在将应用程序移植到 Silverlight Web 应用程序并遇到问题。

我已经包含了代码的简化版本以及引发的错误。在EndGetResponse 方法上抛出错误。

请随时询问更多信息。我四处寻找解决方案,但没有找到任何有效或真正适用于我的问题的东西。看起来如此简单的代码,它在 WP7 版本上完美运行。任何帮助将不胜感激。

代码

void SendRequest() {
    string url = "http://some-example-restful-service:5600/locations/data/all";
    HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(url);

    request.Method = "POST";
    request.ContentType = "application/json; charset=utf-8";

    request.BeginGetResponse(new AsyncCallback(RequestCallback), request);
}

public static void RequestCallback(IAsyncResult asyncResult) {
    HttpWebRequest request = (HttpWebRequest) asyncResult.AsyncState;
    HttpWebResponse response = (HttpWebResponse) request.EndGetResponse(asyncResult);

    //Parse JSON object

    response.Close();

    //Dispatch result back to GUI thread
}

错误

SecurityException 未被用户代码处理

System.Security.SecurityException:安全错误。

at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)
--- End of inner exception stack trace ---
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at ServieTest.MainPage.RequestCallback(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClassd.<InvokeGetResponseCallback>b__b(Object state2)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

【问题讨论】:

  • 当您从浏览器中运行 Silverlight 实现时,它是否有效?

标签: c# silverlight web-applications silverlight-4.0 httpwebrequest


【解决方案1】:

原来我需要在服务域的根目录中添加一个clientaccesspolicy.xml 文件。

以下是文件的内容:

<?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> 

【讨论】:

    【解决方案2】:

    根据this answer,Silverlight(浏览器内)支持您从中请求数据的服务器的cross-domain policy。由于它是您自己的 Web 服务,因此您可以将 crossdomain.xml 文件添加到应用程序的根目录,以允许外部域发出跨域请求(根据 this answer)。

    【讨论】:

    • 我看到了这两个帖子,但我不认为这是服务的问题,因为 WP7 应用程序使用它(Android 和 iPhone 应用程序也使用它)。该服务是围绕一个简单的HttpListener 构建的。我可以尝试添加crossdomain.xml 文件,但我真的不知道如何让它驻留在域的根目录中。
    • 如果服务位于根目录 (http://some-example-restful-service:5600/),那么您可以检查 HttpListenerRequest.Url 中的 "crossdomain.xml" 并使用适当的 XML 进行响应。
    • WP7 不检查跨域策略,因为 WP7 应用程序没有来源并且被认为是受信任的。
    • 好的,我在域的根目录下有crossdomain.xml 文件,如果我只是用浏览器访问它就可以看到它,但我仍然得到应用程序的异常。
    • 我不确定为什么 crossdomain.xml 文件没有解决问题,但您的解决方案确实帮助我弄清楚了我必须做的事情,我将其作为另一个解决方案发布。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多