在Silverlight中覆盖成员时违反了继承安全规则

时间:2012-06-05 05:31:38

标签: silverlight code-access-security

我正在使用Silverlight的Web应用程序。我重载了WebClient.GetWebRequest方法,如下所示: -

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未被用户代码
处理   Message ='SigmaWC.Utility.RestCommunicator'的类型初始值设定项   抛出一个例外。类型名= SigmaWC.Utility.RestCommunicator
  堆栈跟踪:          在SigmaWC.Utility.RestCommunicator..ctor()          在SigmaWC.App..ctor()InnerException:System.TypeLoadException          Message =覆盖成员时违反了继承安全规则:'SigmaWC.Utility.WebClientWithCookies..ctor()'。安全   覆盖方法的可访问性必须与安全性相匹配   被覆盖的方法的可访问性。          堆栈跟踪:               在SigmaWC.Utility.RestCommunicator..cctor()          的InnerException:

任何人都可以帮助提升Silverlight中的安全设置。

1 个答案:

答案 0 :(得分:4)

至少可以说,关于此的文档很少。但是,有一些资源很有用:

MSDN表示您不能将框架成员与SecurityCriticalAttribute一起使用。

  

Silverlight应用程序代码无法使用具有SecurityCriticalAttribute的类型和成员。安全关键类型和成员只能由.NET Framework for Silverlight类库中的受信任代码使用。

对于WebClientGetWebRequest方法没有此属性,但构造函数不具备此属性。

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程序集中忽略了Security属性,而Silverlight程序集不是核心框架的一部分。但是,这可能仅适用于汇编级属性。

无论如何,要长话短说。 You cannot derive from WebClient because of the SecuritySafeAttribute on the constructor. 为了说明这一点,这也会在运行时导致异常:

public class MyWebClient : WebClient
{

}

另一种方法是推出自己的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);

    }
}