【问题标题】:System.Security.SecurityException in push notification in C#C# 推送通知中的 System.Security.SecurityException
【发布时间】:2013-06-08 14:20:13
【问题描述】:

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

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,布尔 isPermSet) 在 System.Security.CodeAccessPermission.Demand() 在 System.Net.ServicePointManager.set_ServerCertificateValidationCallback(RemoteCertificateValidationCallback 值) 在 c:\users\fatih polat\documents\visual studio 2010\Projects\BitirmeServis\BitirmeServis\AndroidGCMPushNotification.cs 中的 BitirmeServis.AndroidGCMPushNotification.SendGCMNotification(String deviceID, String message, Int32 pushCode, String apiKey, String contentTitle, String err):第 31 行 在 C:\users\fatih polat\documents\visual studio 2010\Projects\BitirmeServis\BitirmeServis\Service1 中的 BitirmeServis.Service1.call_taxi(字符串出租车Id,字符串 userId,字符串 userLatitude,字符串 userLongitude,字符串 targetlat,字符串 targetlon,字符串 onerilenfiyat) .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;
    }
}

【问题讨论】:

    标签: c# asp.net push-notification


    【解决方案1】:

    异常发生在以下行:

    ServicePointManager.ServerCertificateValidationCallback +=
        new RemoteCertificateValidationCallback(ValidateServerCertificate);
    

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

    您的选择:

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

    【讨论】:

    • Thnx micheal 我删除了这些行,现在它工作正常:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 2011-06-01
    • 1970-01-01
    • 2020-06-28
    相关资源
    最近更新 更多