2012-06-05 24 views
2

我正在Silverlight中處理Web應用程序。我已經重載WebClient.GetWebRequest方法下面給出: -重寫Silverlight中的成員時違反了繼承安全規則

public class WebClientWithCookies : WebClient 
    { 
     [SecurityCritical] 
     protected override WebRequest GetWebRequest(Uri address) 
     { 
      string cookieContent = HtmlPage.Document.Cookies; 

      WebRequest request = base.GetWebRequest(address); 
      HttpWebRequest webRequest = request as HttpWebRequest; 
      if (webRequest != null && cookieContent != null && cookieContent != string.Empty) 
      { 
       CookieContainer cookieContainer = new CookieContainer(); 
       cookieContainer.Add(address, new Cookie() { Value = HtmlPage.Document.Cookies }); 
       webRequest.CookieContainer = cookieContainer; 
      } 
      return request; 
     } 
    } 

但我正在以下例外:

System.TypeInitializationException是由用戶代碼
消息=的類型初始未處理'SigmaWC.Utility.RestCommunicator' 拋出異常。類型名= SigmaWC.Utility.RestCommunicator
堆棧跟蹤: 在SigmaWC.Utility.RestCommunicator..ctor() 在SigmaWC.App..ctor()的InnerException:System.TypeLoadException 消息=繼承安全規則而重寫構件違反:' SigmaWC.Utility.WebClientWithCookies..ctor()」。安全 覆蓋方法的可訪問性必須匹配被覆蓋的方法的安全性 。 堆棧跟蹤: 在SigmaWC.Utility.RestCommunicator..cctor() 的InnerException:

可以如何在Silverlight提升安全設置任何人的幫助。

回答

4

關於這方面的文檔很少說。但是,有幾個有用的資源:

MSDN表示您不能將框架成員與SecurityCriticalAttribute一起使用。

具有SecurityCriticalAttribute的類型和成員不能由Silverlight應用程序代碼使用。安全關鍵類型和成員只能由.NET Framework for Silverlight類庫中受信任的代碼使用。

WebClient的情況下,GetWebRequest方法沒有這個屬性,但是構造函數的確如此。

This MSDN Security blog意味着if the default constructor has any Security attribute, the class cannot be used for inheritance in a Silverlight client.

進一步說,上述MSDN博客意味着安全屬性在Silverlight組件不屬於核心框架的一部分被忽略。然而,這可能僅適用於裝配級別屬性。

無論如何,簡而言之,長話短說。 You cannot derive from WebClient because of the SecuritySafeAttribute on the constructor. 爲了說明這一點,這也導致異常在運行時:

public class MyWebClient : WebClient 
{ 

} 

另一種方法是推出自己的Web客戶端。這需要一點點工作,但下面的例子並具有以下處理工作:

public class MyHandler : IHttpHandler 
{ 

    public void ProcessRequest(HttpContext context) 
    { 
     context.Response.ContentType = "text/plain"; 
     context.Response.Write("Hello World"); 
     foreach (Cookie cookie in context.Response.Cookies) 
     { 
      //Cookies from the client - there will be 1 in this case 
     } 
    } 

...

public class MyWebClient 
{ 
    public MyWebClient() 
    { 

    } 

    public void InvokeWebRequest(Uri address) 
    { 
     //Set the cookie you want to use. 
     string cookieContent = "I like cookies"; 

     // Register a http client - without this the following webRequest.CookieContainer setter will throw an exception 
     WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp); 

     //This bit you know, but dont forget to set Name on your new Cookie. 
     HttpWebRequest webRequest = WebRequest.Create(address.AbsoluteUri) as HttpWebRequest; 
     if (webRequest != null && !String.IsNullOrWhiteSpace(cookieContent)) 
     { 
      webRequest.CookieContainer = new CookieContainer(); 
      webRequest.CookieContainer.Add(address, new Cookie() { Value = cookieContent, Name = "MyCookie" }); 
     } 

     //Invoke the async GetResponse method. 
     webRequest.BeginGetResponse(o => 
      { 
       HttpWebResponse response = (HttpWebResponse)webRequest.EndGetResponse(o); 
       using (StreamReader reader = new StreamReader(response.GetResponseStream())) 
       { 
        //Read the result 
        string result = reader.ReadToEnd(); 
       } 

       foreach (Cookie cookie in response.Cookies) 
       { 
        //The cookies returned from the server. 
       } 
      }, null); 

    } 
} 
相關問題