c#推送通知中的System.Security.SecurityException
本文关键字:System Security SecurityException 通知 | 更新日期: 2023-09-27 18:17:34
我有一个Asp.net web服务应用程序,我正在向android设备发送推送通知。当我在本地运行我的web服务时,它工作得很好,但当我尝试在web主机中运行web服务时,我得到了以下错误。知道吗?
这是我的c#类System.Web.Services.Protocols。SoapException:服务器无法处理请求。--> System.Security.SecurityException:请求类型为System.Security.Permissions的权限。SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.日志含义在System.Security.CodeAccessSecurityEngine。检查对象需求,StackCrawlMark&stackMark, Boolean isPermSet在System.Security.CodeAccessPermission.Demand ()在System.Net.ServicePointManager。set_ServerCertificateValidationCallback (RemoteCertificateValidationCallback价值)在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在BitirmeServis.Service1。call_taxi(字符串taxid,字符串userId,字符串userLatitude,字符串userLongitude,字符串targetlat,字符串targetlon,字符串onerilenfiyat) in c:'users'fatih polat'documents'visual studio 2010'Projects'BitirmeServis'BitirmeServis'Service1.asmx.cs:line 97失败的行动是:需求第一个失败的权限类型是:System.Security.Permissions.SecurityPermission第一个失败的权限是:
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;
}
}
下面一行出现异常:
ServicePointManager.ServerCertificateValidationCallback +=
new RemoteCertificateValidationCallback(ValidateServerCertificate);
要设置ServicePointManager.ServerCertificateValidationCallback
属性,您的应用程序需要带有Infrastructure
标志的SecurityPermission
。. NET主机提供商未授予您此权限。
你的选择:
- 删除上面一行。(无论如何,在生产中忽略证书错误不是一个好主意。)
- 要求您的主机提供商授予您的应用程序所需的权限(这可能需要您购买一个专用服务器),或者找到另一个主机提供商。