2012-06-05 4 views
2

저는 Silverlight에서 웹 응용 프로그램을 작성하고 있습니다. 아래에 주어진 나는 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' 이 예외를 던졌습니다. TYPENAME = SigmaWC.Utility.RestCommunicator
스택 트레이스 : SigmaWC.App..ctor에서 SigmaWC.Utility.RestCommunicator..ctor() ()의 InnerException에서 : 멤버를 무시하면서 System.TypeLoadException이 메시지 = 상속 보안 규칙 위반 ' SigmaWC.Utility.WebClientWithCookies..ctor() '. 보안 재정의 메서드의 액세스 가능성은 보안 메서드의 액세스 가능성을 재정의해야합니다. 스택 트레이스 : SigmaWC.Utility.RestCommunicator..cctor에서 () 의 InnerException :

실버의 보안 설정을 상승하는 방법에 누구의 도움을 할 수 있습니다.

답변

4

이 문제에 관한 문서는 부족합니다. 그러나 다음과 같은 유용한 리소스가 있습니다.

MSDNSecurityCriticalAttribute과 함께 프레임 워크 구성원을 사용할 수 없음을 나타냅니다.

SecurityCriticalAttribute가있는 형식 및 멤버는 Silverlight 응용 프로그램 코드에서 사용할 수 없습니다. 보안에 중요한 형식 및 멤버는 Silverlight 클래스 라이브러리 용 .NET Framework의 신뢰할 수있는 코드에서만 사용할 수 있습니다. 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 블로그는 보안 속성이 코어 프레임 워크의 일부가 아닌 실버 라이트 어셈블리에서 무시되는 것을 의미 있음을 의미한다. 그러나 이것은 어셈블리 레벨 속성에만 적용될 수 있습니다.

어쨌든 긴 이야기를 짧게 줄입니다. 점을 설명하기 위해 You cannot derive from WebClient because of the SecuritySafeAttribute on the constructor. ,이 또한 런타임에 예외가 발생합니다 :

public class MyWebClient : WebClient 
{ 

} 

대안은 자신의 웹 클라이언트를 출시하는 것입니다. 그것은 약간의 작업이 필요하지만 다음의 예는 다음 핸들러로 작업 않습니다 ...

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); 

    } 
}