Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将架构文件打包到dll中以保护它们不被篡改_C#_Xsd Validation - Fatal编程技术网

C# 将架构文件打包到dll中以保护它们不被篡改

C# 将架构文件打包到dll中以保护它们不被篡改,c#,xsd-validation,C#,Xsd Validation,我也有类似的问题 但我的观点首先不是安全问题。我希望保护我的模式文件不受“愚蠢用户”的攻击,而不是真正的攻击者 是否有可能在编译时将我的xsd文件打包到dll中,并在运行时使用它(而不是从文件系统读取文本文件) 如果它在一个dll中,“愚蠢的用户”就不能偶然地编辑文件,对于攻击者,我们甚至可以更进一步,用强大的命名和数字签名保护dll internal class XmlValidator : Validator { private static XmlSchemaSet _schema

我也有类似的问题

但我的观点首先不是安全问题。我希望保护我的模式文件不受“愚蠢用户”的攻击,而不是真正的攻击者

是否有可能在编译时将我的xsd文件打包到dll中,并在运行时使用它(而不是从文件系统读取文本文件)

如果它在一个dll中,“愚蠢的用户”就不能偶然地编辑文件,对于攻击者,我们甚至可以更进一步,用强大的命名和数字签名保护dll

internal class XmlValidator : Validator
{
    private static XmlSchemaSet _schemas;

    /// <summary>
    /// Initializes a new instance of the <see cref="XmlValidator"/> class.
    /// </summary>
    internal XmlValidator()
    {
        string path;
            path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        }
        else
        {
            path = @".\";
        }

        // Add schemas
        _schemas = new XmlSchemaSet();
        _schemas.Add("http://myschema/schema1", Path.Combine(path, "Schemas", "schema-v1.0.xsd"));
        _schemas.Add("http://myschema/schema11", Path.Combine(path, "Schemas", "chema-v1.1.xsd"));

    }
内部类XmlValidator:验证器
{
私有静态XmlSchemaSet_模式;
/// 
///初始化类的新实例。
/// 
内部XmlValidator()
{
字符串路径;
path=path.GetDirectoryName(System.Reflection.Assembly.GetExecutionGassembly().Location);
}
其他的
{
路径=@“\”;
}
//添加架构
_schemaset=新的XmlSchemaSet();
_schemas.Add(“http://myschema/schema1,Path.Combine(路径,“Schemas”,“schema-v1.0.xsd”);
_schemas.Add(“http://myschema/schema11,Path.Combine(Path,“Schemas”,“chema-v1.1.xsd”);
}
因此,我不想在初始化期间直接从文件系统中读取它们,而是想将它们作为某种资源来读取


类似于翻译文件。在编译时创建,在运行时不可更改

确保这是可能的。我用同样的方法来保护他们

首先将它们声明为
嵌入式资源

在代码中使用它

public void LoadXsd()
{
    string resourceName = "DefaultNamespace.specs.info.IErrorInfo.xsd";
    Assembly assembly = Assembly.GetExecutingAssembly();
    XmlSchema xsd = XmlSchema.Read(assembly.GetManifestResourceStream(resourceName), _XsdSchema_ValidationEventHandler);

    XmlSchemaSet schemaSet = new XmlSchemaSet();
    schemaSet.Add(xsd);
}

private void _XsdSchema_ValidationEventHandler(object sender, ValidationEventArgs e)
{
    _Logger.Error($"XSD validation error: {e.Message}");
}
也可以一次加载所有文件:

public void LoadAllXsdFiles()
{
    XmlSchemaSet schemaSet = new XmlSchemaSet();
    var assembly = Assembly.GetExecutingAssembly();
    var allXsdFiles = assembly.GetManifestResourceNames().Where(r => r.EndsWith(".xsd"));
    foreach (string xsdFile in allXsdFiles)
    {
        XmlSchema xsd = XmlSchema.Read(assembly.GetManifestResourceStream(xsdFile), _XsdSchema_ValidationEventHandler);
        schemaSet.Add(xsd);
    }
}