user278618
user278618

Reputation: 20262

SecurityException while creating cookie at silverlight 4 application

I have a problem with WebRequest in Silverlight 4. In method ReadCallback at request.EndGetResponse(result) as HttpWebResponse;

I've got an error about security. I don't know what is a source of this problem. I've added to DownloadUpload.Web(asp.net 4) project 2 files : clientaccesspolicy.xml, and crossdomain.xml as in this article:

http://msdn.microsoft.com/en-us/library/cc197955(v=vs.95).aspx and I still have this error.

This is a class at silverlight application:

public class CookieSample
{
    private const string ServerPath = "http://localhost:8975/";
    private const string CookieService = "CookieService.ashx";

    public static void SendCookie()
    {
        WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);

        var request = (HttpWebRequest)
            WebRequest.Create(ServerPath + CookieService);

        request.CookieContainer = new CookieContainer();

        request.CookieContainer.Add(new Uri(ServerPath),
            new Cookie("mycookie", "Hello"));
        request.BeginGetResponse(ReadCallback, request);
    }

    private static void ReadCallback(IAsyncResult result)
    {
        var request = (HttpWebRequest)result.AsyncState;
        var response = request.EndGetResponse(result)
            as HttpWebResponse;//here I get error
        var stringValue = response.Cookies["mycookie"].Value;
    }
}

This is a class at DownloadUpload.Web project named CookieService.ashx.cs

public class CookieService : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            var cookie = context.Request.Cookies["mycookie"];

            cookie.Value += " success";

            context.Response.ContentType = "text/plain";
            context.Response.Cookies.Add(cookie);
            context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

Exception:

System.Security.SecurityException was unhandled by user code
  Message=""
  StackTrace:
       w System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
       w System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
       w DownloadUpload.Helpers.CookieSample.ReadCallback(IAsyncResult result)
       w System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassd.<InvokeGetResponseCallback>b__b(Object state2)
       w System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
       w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       w System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
       w System.Threading.ThreadPoolWorkQueue.Dispatch()
       w System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
  InnerException: System.Security.SecurityException
       Message=Security error.
       StackTrace:
            w System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
            w System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
            w System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__1(Object sendState)
       InnerException: 

What's wrong with this code?

I've got it from Silverlight 4 in Action book.

Here are paths of files are being used:

C:\Users\Damian\Downloads\DownloadUpload\DownloadUpload.Web\CookieService.ashx.cs C:\Users\Damian\Downloads\DownloadUpload\DownloadUpload.Web\clientaccesspolicy.xml C:\Users\Damian\Downloads\DownloadUpload\DownloadUpload\Helpers\CookieSample.cs

Upvotes: 2

Views: 711

Answers (1)

emp
emp

Reputation: 5065

If you have used the exact copy of the clientaccesspolicy.xml from here, you might want to allow all http-request-headers by specifying a wildcard. I don't think you are using a standard SOAP action call.

  <allow-from http-request-headers="*"> 

Upvotes: 2

Related Questions