C# 从域中获取SPF记录
检查域上SPF记录的方法有哪些 有一个网站,我可以通过以下方式手动执行此操作:C# 从域中获取SPF记录,c#,asp.net,spf,C#,Asp.net,Spf,检查域上SPF记录的方法有哪些 有一个网站,我可以通过以下方式手动执行此操作: 如何通过ASP.NET和C#实现这一点?基本上我想验证/检查域上的SPF记录,如果它支持自己的邮件web服务器。您基本上需要执行DNS请求,请求域的MX/SPF记录。在C#around中有一些这样做的例子。这里有一个库,它有一个带有GetMxRecords的验证器类,您可能会发现它很有用我也有同样的问题,并设法找到了两个和三个解决方案: nslookup解决方案 您可以通过在命令行中键入以下命令来获取SPF: nsl
如何通过ASP.NET和C#实现这一点?基本上我想验证/检查域上的SPF记录,如果它支持自己的邮件web服务器。您基本上需要执行DNS请求,请求域的MX/SPF记录。在C#around中有一些这样做的例子。这里有一个库,它有一个带有
GetMxRecords
的验证器
类,您可能会发现它很有用我也有同样的问题,并设法找到了两个和三个解决方案:
nslookup
解决方案
您可以通过在命令行中键入以下命令来获取SPF:
nslookup -type=TXT <hostname>
尽管.NET对网络有很多支持,包括主机名到地址的映射,但它缺乏查询DNS的通用方法 但是,您可以使用p/Invoke直接调用。API有点麻烦,但为您的需求创建正确的P/Invoke签名并非不可能 SPF记录作为TXT记录存储在DNS中。您必须使用的相应结构是。如果您能找到,您可以重用代码并使用
DNS\u TYPE\u TEXT
作为查询类型,并将数据解组为DNS\u TXT\u数据
结构
或者您可以使用以下代码:
using System.ComponentModel;
using System.Runtime.InteropServices;
public String DnsGetTxtRecord(String name) {
const Int16 DNS_TYPE_TEXT = 0x0010;
const Int32 DNS_QUERY_STANDARD = 0x00000000;
const Int32 DNS_ERROR_RCODE_NAME_ERROR = 9003;
const Int32 DNS_INFO_NO_RECORDS = 9501;
var queryResultsSet = IntPtr.Zero;
try {
var dnsStatus = DnsQuery(
name,
DNS_TYPE_TEXT,
DNS_QUERY_STANDARD,
IntPtr.Zero,
ref queryResultsSet,
IntPtr.Zero
);
if (dnsStatus == DNS_ERROR_RCODE_NAME_ERROR || dnsStatus == DNS_INFO_NO_RECORDS)
return null;
if (dnsStatus != 0)
throw new Win32Exception(dnsStatus);
DnsRecordTxt dnsRecord;
for (var pointer = queryResultsSet; pointer != IntPtr.Zero; pointer = dnsRecord.pNext) {
dnsRecord = (DnsRecordTxt) Marshal.PtrToStructure(pointer, typeof(DnsRecordTxt));
if (dnsRecord.wType == DNS_TYPE_TEXT) {
var lines = new List<String>();
var stringArrayPointer = pointer
+ Marshal.OffsetOf(typeof(DnsRecordTxt), "pStringArray").ToInt32();
for (var i = 0; i < dnsRecord.dwStringCount; ++i) {
var stringPointer = (IntPtr) Marshal.PtrToStructure(stringArrayPointer, typeof(IntPtr));
lines.Add(Marshal.PtrToStringUni(stringPointer));
stringArrayPointer += IntPtr.Size;
}
return String.Join(Environment.NewLine, lines);
}
}
return null;
}
finally {
const Int32 DnsFreeRecordList = 1;
if (queryResultsSet != IntPtr.Zero)
DnsRecordListFree(queryResultsSet, DnsFreeRecordList);
}
}
[DllImport("Dnsapi.dll", EntryPoint = "DnsQuery_W", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]
static extern Int32 DnsQuery(String lpstrName, Int16 wType, Int32 options, IntPtr pExtra, ref IntPtr ppQueryResultsSet, IntPtr pReserved);
[DllImport("Dnsapi.dll")]
static extern void DnsRecordListFree(IntPtr pRecordList, Int32 freeType);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct DnsRecordTxt {
public IntPtr pNext;
public String pName;
public Int16 wType;
public Int16 wDataLength;
public Int32 flags;
public Int32 dwTtl;
public Int32 dwReserved;
public Int32 dwStringCount;
public String pStringArray;
}
使用System.ComponentModel;
使用System.Runtime.InteropServices;
公共字符串DnsGetTxtRecord(字符串名称){
常量Int16 DNS_TYPE_TEXT=0x0010;
const Int32 DNS_QUERY_标准=0x00000000;
const Int32 DNS_ERROR_RCODE_NAME_ERROR=9003;
const Int32 DNS_INFO_NO_RECORDS=9501;
var queryResultsSet=IntPtr.Zero;
试一试{
变量dnsStatus=DnsQuery(
名称
DNS_类型_文本,
DNS\u查询\u标准,
IntPtr.Zero,
参考queryResultsSet,
IntPtr.Zero
);
如果(dnsStatus==DNS_错误_RCODE_NAME_错误| | dnsStatus==DNS_信息_无记录)
返回null;
如果(dnsStatus!=0)
抛出新的Win32Exception(dnsStatus);
dnsRecord TXT dnsRecord;
for(var pointer=queryResultsSet;pointer!=IntPtr.Zero;pointer=dnsRecord.pNext){
dnsRecord=(DnsRecordTxt)Marshal.ptr结构(指针,typeof(DnsRecordTxt));
如果(dnsRecord.wType==DNS\u TYPE\u TEXT){
变量行=新列表();
var stringArrayPointer=指针
+Marshal.OffsetOf(typeof(DnsRecordTxt),“pStringArray”).ToInt32();
对于(变量i=0;i
了解价值-MailBee的.NET对象也支持这一点。我之所以这么说,是因为我们已经拥有了这个组件,而我正要实现一些其他的东西,这时我发现这个好东西已经被烘焙到了我们拥有的东西中
有趣的是,所有的网站都有这样的错误 SPF不是TXT 您可以有一个不带SPF的txt记录和一个不带txt的SPF记录,因此txt查找不会显示SPF,我添加了一些注释来解释发生了什么,并调整为返回多个记录(如果存在) 我的示例还将TXT记录中的多个字符串连接起来,而不是用换行符分隔 考虑到
DnsQuery
函数的参数中存在约束,我不知道if(dnsRecord.wType==DNS\u TYPE\u TEXT)
行是否真的有必要,但我还是从Martin的答案中保留了它
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
namespace Util
{
/// <summary>
/// Based on https://stackoverflow.com/a/11884174 (Martin Liversage)
/// </summary>
class DnsInterop
{
private const short DNS_TYPE_TEXT = 0x0010;
private const int DNS_QUERY_STANDARD = 0x00000000;
private const int DNS_ERROR_RCODE_NAME_ERROR = 9003;
private const int DNS_INFO_NO_RECORDS = 9501;
public static IEnumerable<string> GetTxtRecords(string domain)
{
var results = new List<string>();
var queryResultsSet = IntPtr.Zero;
DnsRecordTxt dnsRecord;
try
{
// get all text records
// pointer to results is returned in queryResultsSet
var dnsStatus = DnsQuery(
domain,
DNS_TYPE_TEXT,
DNS_QUERY_STANDARD,
IntPtr.Zero,
ref queryResultsSet,
IntPtr.Zero
);
// return null if no records or DNS lookup failed
if (dnsStatus == DNS_ERROR_RCODE_NAME_ERROR
|| dnsStatus == DNS_INFO_NO_RECORDS)
{
return null;
}
// throw an exception if other non success code
if (dnsStatus != 0)
throw new Win32Exception(dnsStatus);
// step through each result
for (
var pointer = queryResultsSet;
pointer != IntPtr.Zero;
pointer = dnsRecord.pNext)
{
dnsRecord = (DnsRecordTxt)
Marshal.PtrToStructure(pointer, typeof(DnsRecordTxt));
if (dnsRecord.wType == DNS_TYPE_TEXT)
{
var builder = new StringBuilder();
// pointer to array of pointers
// to each string that makes up the record
var stringArrayPointer = pointer + Marshal.OffsetOf(
typeof(DnsRecordTxt), "pStringArray").ToInt32();
// concatenate multiple strings in the case of long records
for (var i = 0; i < dnsRecord.dwStringCount; ++i)
{
var stringPointer = (IntPtr)Marshal.PtrToStructure(
stringArrayPointer, typeof(IntPtr));
builder.Append(Marshal.PtrToStringUni(stringPointer));
stringArrayPointer += IntPtr.Size;
}
results.Add(builder.ToString());
}
}
}
finally
{
if (queryResultsSet != IntPtr.Zero)
{
DnsRecordListFree(queryResultsSet,
(int)DNS_FREE_TYPE.DnsFreeRecordList);
}
}
return results;
}
[DllImport("Dnsapi.dll", EntryPoint = "DnsQuery_W",
ExactSpelling = true, CharSet = CharSet.Unicode,
SetLastError = true)]
static extern int DnsQuery(string lpstrName, short wType, int options,
IntPtr pExtra, ref IntPtr ppQueryResultsSet, IntPtr pReserved);
[DllImport("Dnsapi.dll")]
static extern void DnsRecordListFree(IntPtr pRecordList, int freeType);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct DnsRecordTxt
{
public IntPtr pNext;
public string pName;
public short wType;
public short wDataLength;
public int flags;
public int dwTtl;
public int dwReserved;
public int dwStringCount;
public string pStringArray;
}
enum DNS_FREE_TYPE
{
DnsFreeFlat = 0,
DnsFreeRecordList = 1,
DnsFreeParsedMessageFields = 2
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Runtime.InteropServices;
使用系统文本;
命名空间Util
{
///
///基于https://stackoverflow.com/a/11884174 (马丁·利弗萨奇)
///
DnsInterop类
{
private const short DNS_TYPE_TEXT=0x0010;
私有常量int DNS\u QUERY\u标准=0x00000000;
私有常量int DNS_ERROR_RCODE_NAME_ERROR=9003;
私有常量int DNS\u信息\u无记录=9501;
公共静态IEnumerable GetTxtRecords(字符串域)
{
var results=新列表();
var queryResultsSet=IntPtr.Zero;
dnsRecord TXT dnsRecord;
尝试
{
//获取所有文本记录
//在queryResultsSet中返回指向结果的指针
变量dnsStatus=DnsQuery(
领域,
DNS_类型_文本,
DNS\u查询\u标准,
IntPtr.Zero,
参考queryResultsSet,
IntPtr.Zero
);
//如果没有记录或DNS查找失败,则返回null
如果(dnsStatus==DNS\u错误\u代码\u名称\u错误
||dnsStatus==DNS\u信息\u无记录)
{
返回null;
}
//如果其他代码不成功,则引发异常
如果(dnsSt
public String GetSpfRecord(String domain)
{
// Definition of DNS params
const Int16 DNS_TYPE_TXT = 0x0010;
const Int32 DNS_QUERY_STANDARD = 0x00000001;
const Int32 DNS_ERROR_RCODE_NAME_ERROR = 9003;
const Int32 DNS_INFO_NO_RECORDS = 9501;
DnsRecordA dnsRecord;
var queryResultsSet = IntPtr.Zero;
try
{
var dnsStatus = DnsQuery(
domain,
DNS_TYPE_TXT,
DNS_QUERY_STANDARD,
IntPtr.Zero,
ref queryResultsSet,
IntPtr.Zero
);
if (dnsStatus == DNS_ERROR_RCODE_NAME_ERROR || dnsStatus == DNS_INFO_NO_RECORDS)
return null;
if (dnsStatus != 0)
throw new Win32Exception(dnsStatus);
for (IntPtr pointer = queryResultsSet; pointer != IntPtr.Zero; pointer = dnsRecord.pNext)
{
// Copies data from memory (size of DnsRecordA) from adress pointer to new alocated memory and creates instance of pointer to this place.
dnsRecord = (DnsRecordA)Marshal.PtrToStructure(pointer, typeof(DnsRecordA));
// pokud se jedná o typ TXT
if (dnsRecord.wType == DNS_TYPE_TXT)
{
// get pointer to informations in "Data" property (https://docs.microsoft.com/en-us/windows/win32/api/windns/ns-windns-dns_recorda)
var dataPointer = pointer + Marshal.SizeOf(typeof(DnsRecordA));
// Get the txtData
var txtData = (DNS_TXT_DATAA)Marshal.PtrToStructure(dataPointer, typeof(DNS_TXT_DATAA));
if (txtData.dwStringCount >= 1)
{
string line = Marshal.PtrToStringUni(txtData.pStringArray[0]);
// only if record starts with "v=spf" (Getting only SPF records)
// Getting only first (here is always maximum of 1 record) and returning whole line
if (line.StartsWith("v=spf") && string.IsNullOrEmpty(result))
{
return line;
}
}
}
}
// no SPF record - returning null
return null;
}
finally
{
const Int32 DnsFreeRecordList = 1;
// always release the memory alocated for list of dns records
if (queryResultsSet != IntPtr.Zero)
DnsRecordListFree(queryResultsSet, DnsFreeRecordList);
}
}
// https://docs.microsoft.com/en-us/windows/win32/api/windns/nf-windns-dnsquery_a
[DllImport("Dnsapi.dll", EntryPoint = "DnsQuery_W", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]
static extern Int32 DnsQuery(String lpstrName, Int16 wType, Int32 options, IntPtr pExtra, ref IntPtr ppQueryResultsSet, IntPtr pReserved);
// https://docs.microsoft.com/en-us/windows/win32/api/windns/nf-windns-dnsrecordlistfree
[DllImport("Dnsapi.dll")]
static extern void DnsRecordListFree(IntPtr pRecordList, Int32 freeType);
// https://docs.microsoft.com/en-us/windows/win32/api/windns/ns-windns-dns_recorda
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct DnsRecordA
{
public IntPtr pNext;
public String pName;
public Int16 wType;
public Int16 wDataLength;
public Int32 flags;
public Int32 dwTtl;
public Int32 dwReserved;
// Commented, because i'm getting this value dynamicaly (it can also be another structure type which might cause some problems)
//public DNS_TXT_DATA Data;
}
// https://docs.microsoft.com/en-us/windows/win32/api/windns/ns-windns-dns_txt_dataa
[StructLayout(LayoutKind.Sequential)]
struct DNS_TXT_DATAA
{
/// DWORD->unsigned int
public uint dwStringCount;
/// PSTR[1]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1, ArraySubType = UnmanagedType.SysUInt)]
internal IntPtr[] pStringArray;
}