我想改变默认的ASP.NET 4编码器运行UbuntuLinux16.04操作系统的联想Thinkstation桌面和apache服务器2.0和mod-mono-server4.exe。
我的问题是,下面的配方是否与安装在Ubuntu16.04上的.NET Mono4.0库一起正常工作。与此相比,这种方法的优点是什么,因为我们使用的.NET版本早于4.5(即ASP.NET 4.0),其中我们使用以下encoderType设置。
<httpRuntime encoderType="Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary" />这已经通过安装在Ubuntu16.04上的.NET Mono4.0库被证明是正确的。
Jardine于2011年7月9日发布了这个网址中的菜谱,名为[https://www.jardinesoftware.net/2011/07/09/asp-net-4-change-the-default-encoder/] 4:更改默认编码器
引用这篇好文章的话,“在ASP.Net 4.0中,微软增加了覆盖默认编码器的能力。这是专门关注HTMLEncode、HTMLAttributeEncode和URLEncode功能的。从安全性的角度来看,这些功能被用来帮助减轻跨站点脚本(XSS)。内置的.Net例程的问题是,它们是建立在黑名单方法上的,而不是白名单方法。内置例程使用非常小的字符列表进行编码。例如,.Net版本的HTMLEncode编码了以下字符:<、>、“&。Microsoft Web保护库(以前称为“反XSS库”)将确定不需要编码的所有字符,例如a-z0-9,然后对所有其他字符进行编码。这是一种更安全的编码方法。
在这篇文章中,我将向您展示如何使用作为ASP.Net 4.0应用程序的默认编码器。第一步是下载Web保护库。在本例中,我使用了Version4.0,它可以在:http://wpl.codeplex.com/上找到。
接下来,您需要有一个应用程序来实现这一点。您可以使用现有的应用程序,也可以创建新的应用程序。添加对AntiXSSLibrary.dll的引用。
要使用库,现在是创建一个新类的时候了。您可以在图1中看到类中的代码,我将类命名为“MyEncoder”,这只是一个示例。(这不是生产代码)这个类有两个重要的因素:
图1
using System;
using System.Web;
public class MyEncoder : System.Web.Util.HttpEncoder
{
public MyEncoder(){}
protected override void HtmlEncode(string value, System.IO.TextWriter output)
{
if (null == value)
return;
output.Write(Microsoft.Security.Application.Encoder.HtmlEncode(value));
}
protected override void HtmlAttributeEncode(string value, System.IO.TextWriter output)
{
if (null == value)
return;
output.Write(Microsoft.Security.Application.Encoder.HtmlAttributeEncode(value));
}
}实现此自定义编码的最后一步是更新Web.config文件。为此,修改httpRuntime元素以设置“encoderType”属性集,如图2所示。将“MyEncoder”更改为您创建的类的名称。如果您没有httpRuntime元素,只需添加它。
图2
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<httpRuntime encoderType="MyEncoder"/>
</system.web>发布于 2016-07-05 02:26:28
是的,自定义编码C#类将与安装在Ubuntu16.04上的Mono .NET 4.0库一起正常工作。
在与.NET 4.0 HttpEncoder特性进行斗争之后,将在
[https://msdn.microsoft.com/en-us/library/system.web.util.httpencoder(v=vs.100).aspx](https://msdn.microsoft.com/en-us/library/system.web.util.httpencoder(v=vs.100%29.aspx])
一个小时以来,我得到了一个例外:
The actual exception which was being reported was:
System.Web.HttpException: Code generation failed. ---> System.Configuration.ConfigurationErrorsException: Could not load type 'AntiXssEncoder, AntiXssLibrary'.
at System.Web.Util.HttpEncoder.GetCustomEncoderFromConfig () <0x40e99f10 + 0x0015f> in <filename unknown>:0
at System.Lazy`1[T].CreateValue () <0x40e8cc80 + 0x001d2> in <filename unknown>:0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <0x40ea1490 + 0x00033> in <filename unknown>:0
at System.Lazy`1[T].get_Value () <0x40e8c580 + 0x000cc> in <filename unknown>:0
at System.Web.Util.HttpEncoder.get_Current () <0x40e99c50 + 0x0006f> in <filename unknown>:0
at System.Web.HttpUtility.HtmlEncode (System.String s) <0x40e99b80 + 0x00053> in <filename unknown>:0
at System.Web.HttpException.HtmlEncode (System.String s) <0x40e99ae0 + 0x00047> in <filename unknown>:0
at System.Web.HttpException.FormatFullStackTrace () <0x40e98510 + 0x001cf> in <filename unknown>:0
at System.Web.HttpException.GetHtmlErrorMessage () <0x40e968f0 + 0x001d3> in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Web.Compilation.BuildManager.GenerateAssembly (System.Web.Compilation.AssemblyBuilder abuilder, System.Web.Compilation.BuildProviderGroup group, System.Web.VirtualPath vp, Boolean debug) <0x40eb3f10 + 0x0045f> in <filename unknown>:0
at System.Web.Compilation.BuildManager.BuildInner (System.Web.VirtualPath vp, Boolean debug) <0x40ea7280 + 0x0057b> in <filename unknown>:0
at System.Web.Compilation.BuildManager.Build (System.Web.VirtualPath vp) <0x40ea6c90 + 0x0014b> in <filename unknown>:0
at System.Web.Compilation.BuildManager.GetCompiledType (System.Web.VirtualPath virtualPath) <0x40ea6940 + 0x00093> in <filename unknown>:0
at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath (System.Web.VirtualPath virtualPath, System.Type requiredBaseType) <0x40ea6800 + 0x00033> in <filename unknown>:0
at System.Web.UI.PageParser.GetCompiledPageInstance (System.String virtualPath, System.String inputFile, System.Web.HttpContext context) <0x40ea6460 + 0x000bb> in <filename unknown>:0
at System.Web.UI.PageHandlerFactory.GetHandler (System.Web.HttpContext context, System.String requestType, System.String url, System.String path) <0x40ea6420 + 0x00027> in <filename unknown>:0
at System.Web.HttpApplication.GetHandler (System.Web.HttpContext context, System.String url, Boolean ignoreContextHandler) <0x40e7e970 + 0x002de> in <filename unknown>:0
at System.Web.HttpApplication.GetHandler (System.Web.HttpContext context, System.String url) <0x40e7e940 + 0x0001b> in <filename unknown>:0
at System.Web.HttpApplication+<Pipeline>c__Iterator1.MoveNext () <0x40e80000 + 0x02a5f> in <filename unknown>:0 因此,我研究了c# Type.GetType(typename,false)中的[https://stackoverflow.com/questions/31613899/why-do-i-have-to-specify-the-assembly-in-my-app-config-but-not-for-my-web-config],@rene写道,修正了这个异常:
"If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace."
"It is adviced to always specify the assemblyname. In the asp.net scenario, if the assemblyname is present in the type parameter it short-circuits to the Type.GetType call which prevents the loading and inspection of all dll's in the bin folder of your webapp."
1. Compiing:
sudo mcs -t:library -r:../bin/System.dll -r:/usr/lib/mono/gac/System.Web/4.0.0.0__b03f5f7f11d50a3a/System.Web.dll -r:../bin/AntiXSSLibrary.dll MyEncoder.cs它构建程序集MyEncoder.dll,我将其复制到bin文件夹中,并将其添加为对csproj的外部引用。请不要将System.Web.dll放在ASP.NET应用程序的bin文件夹中。
https://unix.stackexchange.com/questions/293808
复制相似问题