本文介绍了如何解决.NET Framework 4.0中的DnsRecordListFree错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个MxRecordLookup。我在.NET Framework 4.0中调用DnsRecordListFree时收到错误。我使用Windows 7.我如何解决它?以下是错误:



System.MethodAccessException:尝试通过安全透明方法通过方法调用本机代码。



这是我的代码:

  [DllImport(dnsapi,EntryPoint =DnsQuery_W,CharSet = CharSet.Unicode,SetLastError = true,ExactSpelling = true)] 
private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)] ref string pszName,QueryTypes wType,QueryOptions options,int aipServers,ref IntPtr ppQueryResults,int pReserved);

[DllImport(dnsapi,CharSet = CharSet.Auto,SetLastError = true)]
private static extern void DnsRecordListFree(IntPtr pRecordList,int FreeType);

public List< string> GetMXRecords(string domain)
{
List< string> records = new List< string>();
IntPtr ptr1 = IntPtr.Zero;
IntPtr ptr2 = IntPtr.Zero;
MXRecord recMx;

try
{

int result = DnsQuery(ref domain,QueryTypes.DNS_TYPE_MX,QueryOptions.DNS_QUERY_BYPASS_CACHE,0,ref ptr1,0);
if(result!= 0)
{
if(result == 9003)
{
//没有记录
}
else
{
//其他错误
}
}

for(ptr2 = ptr1;!ptr2.Equals(IntPtr.Zero); ptr2 = recMx.pNext)
{
recMx =(MXRecord)Marshal.PtrToStructure(ptr2,typeof(MXRecord));
if(recMx.wType == 15)
{
records.Add(Marshal.PtrToStringAuto(recMx.pNameExchange));
}
}
}
finally
{
DnsRecordListFree(ptr1,0);
}

返回记录;
}


解决方案

阅读了解透明和关键代码是什么。默认情况下,在.NET 4中,标记有 AllowPartiallyTrustedCallersAttribute 的程序集中的任何代码是透明的,这意味着它不能调用关键代码(标记为 SecurityCriticalAttribute )。只有安全安全关键(标有 SecuritySafeCriticalAttribute )或关键代码才能调用关键代码。



透明可以调用透明或安全性安全严重

安全安全严重可以调用安全安全严重或严重

Critical可以调用严重



启用代码分析Microsoft安全规则设置为查看有关不良安全调用的警告。请注意,您可以通过应用 [assembly:SecurityRules(SecurityRuleSet.Level1)] 或删除 AllowPartiallyTrustedCallersAttribute 。有关详细信息,请参见。



为符合新规则, GetMXRecords 应成为 SecuritySafeCritical DLL导入调用必须标有 SecurityCritical


I am doing an MxRecordLookup. I am getting an error when calling the DnsRecordListFree in the .NET Framework 4.0. I am using Windows 7. How do I get around it? Here is the error:

System.MethodAccessException: Attempt by security transparent method to call native code through method.

Here is my code:

    [DllImport("dnsapi", EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
    private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string pszName, QueryTypes wType, QueryOptions options, int aipServers, ref IntPtr ppQueryResults, int pReserved);

    [DllImport("dnsapi", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);

    public List<string> GetMXRecords(string domain)
    {
        List<string> records = new List<string>();
        IntPtr ptr1 = IntPtr.Zero;
        IntPtr ptr2 = IntPtr.Zero;
        MXRecord recMx;

        try
        {

            int result = DnsQuery(ref domain, QueryTypes.DNS_TYPE_MX, QueryOptions.DNS_QUERY_BYPASS_CACHE, 0, ref ptr1, 0);
            if (result != 0)
            {
                if (result == 9003)
                {
                    //No Record Exists
                }
                else
                {
                    //Some other error
                }
            }

            for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = recMx.pNext)
            {
                recMx = (MXRecord)Marshal.PtrToStructure(ptr2, typeof(MXRecord));
                if (recMx.wType == 15)
                {
                    records.Add(Marshal.PtrToStringAuto(recMx.pNameExchange));
                }
            }
        }
        finally
        {
            DnsRecordListFree(ptr1, 0);
        }

        return records;
    }
解决方案

Read Security Changes in the .NET Framework 4 to understand what transparent and critical code is. By default, in .NET 4, any code in an assembly marked with AllowPartiallyTrustedCallersAttribute is transparent, which means it cannot call critical code (code marked with SecurityCriticalAttribute). Only security safe critical (marked with SecuritySafeCriticalAttribute) or critical code can call critical code. And security safe critical can be called by transparent called.

In short:

Transparent can call Transparent or Security Safe Critical
Security Safe Critical can call Security Safe Critical or Critical
Critical can call Critical

Enable Code Analysis with the Microsoft Security rules set to see warnings about bad security calls. Note that you can revert back to how security worked in .NET Framework 2.0 (all code is critical) by applying [assembly: SecurityRules(SecurityRuleSet.Level1)] or removing AllowPartiallyTrustedCallersAttribute. See Security-Transparent Code, Level 2 for more information.

To comply with the new rules, GetMXRecords should become SecuritySafeCritical and the DLL import calls must be marked with SecurityCritical.

这篇关于如何解决.NET Framework 4.0中的DnsRecordListFree错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 00:59