0
votes

I have an Asp.net web service application and i am sending push notifications to android devices. When i run my web service in local it works fine but when i try run webservice in a web host i got the following errors. Any idea ?

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) at System.Security.CodeAccessPermission.Demand() at System.Net.ServicePointManager.set_ServerCertificateValidationCallback(RemoteCertificateValidationCallback value) at BitirmeServis.AndroidGCMPushNotification.SendGCMNotification(String deviceID, String message, Int32 pushCode, String apiKey, String contentTitle, String err) in c:\users\fatih polat\documents\visual studio 2010\Projects\BitirmeServis\BitirmeServis\AndroidGCMPushNotification.cs:line 31 at BitirmeServis.Service1.call_taxi(String taxiId, String userId, String userLatitude, String userLongitude, String targetlat, String targetlon, String onerilenfiyat) in c:\users\fatih polat\documents\visual studio 2010\Projects\BitirmeServis\BitirmeServis\Service1.asmx.cs:line 97 The action that failed was: Demand The type of the first permission that failed was: System.Security.Permissions.SecurityPermission The first permission that failed was:

here is my C# class

 public static string SendGCMNotification(string deviceID, string message, int pushCode, string apiKey, string contentTitle, string err)
    {
        //string apiKey = Constants.GCM_BROWSER_API_KEY;
        string postDataContentType = "application/json";
        // contentTitle= Constants.PUSH_CODE_STRS[pushCode]
        string postData =
        "{ \"registration_ids\": [ \"" + deviceID + "\" ], " +
          "\"data\": {\"pushCode\":\"" + pushCode.ToString() + "\", " +
                     "\"contentTitle\":\"" + contentTitle + "\", " +
                     "\"message\": \"" + message + "\"}}";

        ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);

        //  MESSAGE CONTENT
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        //
        //  CREATE REQUEST
        HttpWebRequest Request = null;
        try
        {
            Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
        }
        catch (Exception ex)
        {
            err = ex.ToString();
            return "error";
        }

        if (Request == null)
        {
            err = "Boş Request";
            return "error";
        }

        Request.Method = "POST";
        Request.KeepAlive = false;
        Request.ContentType = postDataContentType;
        Request.Headers.Add(string.Format("Authorization: key={0}", apiKey));
        Request.ContentLength = byteArray.Length;

        Stream dataStream = null;
        try
        {
            dataStream = Request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
        }
        catch (Exception ex)
        {
            err = ex.ToString();
            return "error";
        }


        try
        {
            WebResponse Response = Request.GetResponse();
            HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
            if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
            {
                var text = "Unauthorized - need new token";
            }
            else if (!ResponseCode.Equals(HttpStatusCode.OK))
            {
                var text = "Response from web service isn't OK";
            }

            StreamReader Reader = new StreamReader(Response.GetResponseStream());
            string responseLine = Reader.ReadToEnd();
            Reader.Close();
            return responseLine;
        }
        catch (Exception ex)
        {
            err = ex.ToString();
            return "error";
        }

    }


    public static bool ValidateServerCertificate(
                                                object sender,
                                                X509Certificate certificate,
                                                X509Chain chain,
                                                SslPolicyErrors sslPolicyErrors)
    {
        return true;
    }
}
1

1 Answers

1
votes

The exception is occurring on the following line:

ServicePointManager.ServerCertificateValidationCallback +=
    new RemoteCertificateValidationCallback(ValidateServerCertificate);

To set the ServicePointManager.ServerCertificateValidationCallback property, your application needs SecurityPermission with the Infrastructure flag, but your ASP.NET hosting provider has not granted you this permission.

Your options:

  • Delete the line above. (It's not a good idea to ignore certificate errors in production anyway.)
  • Ask your hosting provider to grant your application the required permission (which will probably require that you purchase a dedicated server), or find another hosting provider.