0

I am getting System.Security.SecurityException while trying to make a call to an https endpoint [ Not a web service but a .Net web api hosted on a server ] I added a clientaccesspolicy file with both http and https as

        <?xml version="1.0" encoding="UTF-8"?>

    <access-policy>


    <cross-domain-access>


    <policy>


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

    <domain uri="https://*"/>

    <domain uri="http://*"/>

    </allow-from>


    <grant-to>

    <resource include-subpaths="true" path="/"/>

    </grant-to>

    </policy>

    </cross-domain-access>

    </access-policy>

Corssdomain xml looks like this

  <?xml version="1.0"?>

<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">

<cross-domain-policy>

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

</cross-domain-policy>

The point where I am getting exception is when trying to read the response

HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);

The exception details are here

 {System.Security.SecurityException ---> System.Security.SecurityException: Security error.
at System.Net.Browser.Brow​serHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClassa.<EndGetResponse>b__9(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 SLLAbapp.Views.ChildWindowConfirmExport.ResponseReady(IAsyncResult asyncResult)}

The code I am using to do this POST is like

private void OKButton_Click(object sender, RoutedEventArgs e)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://....."); //url of web api with https
    request.Method = "POST";
    request.ContentType = "application/json";
    request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);
}
void RequestReady(IAsyncResult asyncResult) 
{ 
    HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest; 
    Stream stream = request.EndGetRequestStream(asyncResult); 
    this.Dispatcher.BeginInvoke(delegate() 
    { 
        // Send the post variables 
        StreamWriter writer = new StreamWriter(stream);                
        writer.Write(this.JsonData); //passing actual json data here
        writer.Flush(); 
        writer.Close(); 
        request.BeginGetResponse(new AsyncCallback(ResponseReady), request);               
    }); 
}
void ResponseReady(IAsyncResult asyncResult) 
{
    try
    {
        HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);//getting exception here
        this.Dispatcher.BeginInvoke(delegate()
        {
            if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created)
            {
                // get the result text 
            }

        });
    }
    catch
    {

    }
}

Any idea why this error happends? I also placed the crossdomain.xml on root of web api

Is it because of ssl? HOw can I mention details of ssl on httpWebrequest ??

Sebastian
  • 4,625
  • 17
  • 76
  • 145
  • Are you sure that your `crossdomain.xml` file is [well-formed](http://stackoverflow.com/a/215346/969278)? – Halis S. Sep 08 '16 at 13:43
  • @hellowstone xml contents added on question.. copied from root of my webapi. PLease have a look – Sebastian Sep 08 '16 at 13:47
  • Can you try the [most liberate version](http://stackoverflow.com/a/213272/969278) of crossdomain.xml for debug purposes? Then you can narrow configuration later. – Halis S. Sep 08 '16 at 14:06
  • @hellowstone Have you seen anything wrong in the code or the other xml file? – Sebastian Sep 08 '16 at 15:27
  • I don't see anythıng wrong on code, but as I said before, you'd rather try to change `crossdomain.xml`, especially add `` line. – Halis S. Sep 08 '16 at 17:56
  • @hellowstone Yes nothing wrong in the code . After struggling fro a long time I restarted my machine and then open visual studio and rebuild Silverlight project and it starts working . I haven't replaced the crossdomain.xml file as well – Sebastian Sep 09 '16 at 12:35
  • Happy news for you! Thanks for feedback. – Halis S. Sep 09 '16 at 13:45

0 Answers0