C#中的推送通知中的System.Security.SecurityException

时间:2013-06-08 14:20:14

标签: c# asp.net push-notification

我有一个Asp.net Web服务应用程序,我正在向Android设备发送推送通知。当我在本地运行我的Web服务它工作正常,但当我尝试在Web主机中运行webservice时,我得到以下错误。任何的想法 ?

  

System.Web.Services.Protocols.SoapException:服务器无法处理请求。 ---> System.Security.SecurityException:对类型'System.Security.Permissions.SecurityPermission,mscorlib,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'的权限的请求失败。      在System.Security.CodeAccessSecurityEngine.Check(对象需求,StackCrawlMark& stackMark,Boolean isPermSet)      在System.Security.CodeAccessPermission.Demand()      在System.Net.ServicePointManager.set_ServerCertificateValidationCallback(RemoteCertificateValidationCallback value)      at BitirmeServis.AndroidGCMPushNotification.SendGCMNotification(String deviceID,String message,Int32 pushCode,String apiKey,String contentTitle,String err)在c:\ users \ fatih polat \ documents \ visual studio 2010 \ Projects \ BitirmeServis \ BitirmeServis \ AndroidGCMPushNotification.cs:第31行      at BitirmeServis.Service1.call_taxi(String taxiId,String userId,String userLatitude,String userLongitude,String targetlat,String targetlon,String onerilenfiyat)在c:\ users \ fatih polat \ documents \ visual studio 2010 \ Projects \ BitirmeServis \ BitirmeServis \ Service1 .asmx.cs:第97行   失败的行动是:   需求   失败的第一个权限的类型是:   System.Security.Permissions.SecurityPermission   失败的第一个权限是:   

这是我的C#类

 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 个答案:

答案 0 :(得分:1)

例外情况发生在以下一行:

ServicePointManager.ServerCertificateValidationCallback +=
    new RemoteCertificateValidationCallback(ValidateServerCertificate);

要设置ServicePointManager.ServerCertificateValidationCallback属性,您的应用程序需要SecurityPermission并带有Infrastructure标记,但您的ASP.NET托管服务提供商未授予您此权限。

您的选择:

  • 删除上面的行。 (无论如何,忽略生产中的证书错误并不是一个好主意。)
  • 请您的托管服务提供商向您的应用程序授予所需的权限(可能需要您购买专用服务器),或者找到其他托管服务提供商。