0

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

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(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) 在 System.Security.CodeAccessPermission.Demand() 在 System.Net.ServicePointManager.set_ServerCertificateValidationCallback(RemoteCertificateValidationCallback 值) 在 BitirmeServis.AndroidGCMPushNotification.SendGCMNotification(String deviceID, c中的字符串消息,Int32 pushCode,字符串apiKey,字符串内容标题,字符串错误):

这是我的 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;
    }
}
4

1 回答 1

1

异常发生在以下行:

ServicePointManager.ServerCertificateValidationCallback +=
    new RemoteCertificateValidationCallback(ValidateServerCertificate);

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

您的选择:

  • 删除上面的行。(无论如何,忽略生产中的证书错误并不是一个好主意。)
  • 请您的托管服务提供商授予您的应用程序所需的权限(这可能需要您购买专用服务器),或寻找其他托管服务提供商。
于 2013-06-08T18:14:18.217 回答