下面是我的代码,问题是当我输入任何带有撇号的用户名时(')当返回搜索结果时,会显示附加字符“\ backslash”。
下面是我的代码,我发现 checkusername 函数中使用了 addslashes 函数,因此反斜杠会被添加。
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "pisosA")]
public String devolverPisosA()
{
List<pisosAlquiler> consultaPA = (from piso in contexto.pisosAlquiler
select piso).ToList();
return consultaPA;
}
<system.serviceModel>
<services>
<service name="ProyectoJosephWCF.Service1" behaviorConfiguration="ServiceBehavior">
<endpoint binding="webHttpBinding"
contract="ProyectoJosephWCF.IService1"
behaviorConfiguration="webHttp"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior" >
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
注意:我谷歌并发现它用于安全原因 因为在我的情况下,我们只显示搜索结果。所以我不确定为什么在这里使用这个功能。 我的修复是在返回之前添加 stripslashes ()函数,这将删除addslashes()函数添加的反斜杠。 请在下面找到代码段和代码更改注释:
if ( 0 < count( $my_field_place ))
{
for ( $i = 0; $i < count( $my_field_place ); $i++ )
{
if ( true === isset( $Fields[$i] ))
{
print "gMapping[$i] = new MappingItem( '" .
addslashes( $Fields[$i] ) .
"', '" .
checkusername( $my_field_place[$i] ) .
"' );";
}
}
}
function checkusername($inStr)
{
$orig = array();
$new = array();
$orig[00] = "/\n/`" ; $new[00] = "\\?n";
$orig[01] = "/[^\x-*()]/"; $new[01] = "";
$var1 = preg_replace($orig, $new, $inStr);
$var2 = addslashes($var1 ); // i am not sure why addslashes is used but i am asked not to remove because of security reasion?
return $var2;
}
请帮助添加 stripslashes ,还是有其他方法可以处理它?</ p>
答案 0 :(得分:0)
您可以限制用户使用这些特殊字符 虽然在gmail,yahoo,fb等中他们永远不会 允许这个字符。因为t允许以一种可读的方式表示多个单词 下面是一些文档 https://support.google.com/a/answer/33386?hl=en 见第二条指南