Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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# 以编程方式将IP或域添加到IIS 6上SMTP虚拟服务器中的中继限制_C#_Iis_Smtp - Fatal编程技术网

C# 以编程方式将IP或域添加到IIS 6上SMTP虚拟服务器中的中继限制

C# 以编程方式将IP或域添加到IIS 6上SMTP虚拟服务器中的中继限制,c#,iis,smtp,C#,Iis,Smtp,我的任务是创建一个管理页面,使用C#以编程方式将IP或域添加到SMTP中继限制。从昨天开始,我花了一些时间研究这个问题,最后在几个使用System.DirectoryServices.DirectoryEntr类的网站上找到了一些帖子。使用我找到的示例,我可以在连接控制下添加或拒绝IP或域,但不受中继限制。在C#中,将IP添加到中继限制的命令是什么?以下是IIS中GUI的图片,供参考- 经过大量研究,试图找到在IIS中访问中继限制列表的最佳方法,我想出了这个类。这对于我们正在尝试做的事情非常有

我的任务是创建一个管理页面,使用C#以编程方式将IP或域添加到SMTP中继限制。从昨天开始,我花了一些时间研究这个问题,最后在几个使用System.DirectoryServices.DirectoryEntr类的网站上找到了一些帖子。使用我找到的示例,我可以在连接控制下添加或拒绝IP或域,但不受中继限制。在C#中,将IP添加到中继限制的命令是什么?以下是IIS中GUI的图片,供参考-


经过大量研究,试图找到在IIS中访问中继限制列表的最佳方法,我想出了这个类。这对于我们正在尝试做的事情非常有用,所以请随意使用代码并根据您的使用进行调整。我送给StackOverflow开发人员的礼物,因为他们每天都为我的问题提供了很好的答案

我发现这个解决方案是解决我的问题最简单、最干净的方法。我使用它在IIS的中继限制列表中插入和检索IPs和DNS条目,但它也可以用于在IIS的连接控件中插入和检索条目

注意:IIS不允许删除列表中的一个条目,因此我必须删除所有条目,然后将整个列表重新添加。我找不到只删除一个条目的方法

using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Text;
using Ucf.Smtp.Wcf.Entities;
using System.Reflection;

namespace Ucf.Smtp.Wcf.BusinessLogic
{

public static class IisIntegration
{
    private static object _oIpSecurity;
    private static Type _typeIpSecurityType;

    /// <summary>
    /// Sets IP|Domain into SMTP Server
    /// This method is used to insert one IP or DNS entry into the Relay Restriction List in IIS
    /// </summary>
    /// <param name="sMetabasePath">IIS://localhost/smtpsvc/1</param>
    /// <param name="sMethodName">Get|Put</param>
    /// <param name="sMethodArgument">IPSecurity|RelayIPList</param>
    /// <param name="sMember">IPGrant|IPDeny|DomainGrant|DomainDeny</param>
    /// <param name="item">IP|Domain</param>
    /// <param name="listCurrent">List of current IP(s)|Domain(s)</param>
    /// <param name="aListNew">List of new IP(s)|Domain(s)</param>
    /// <returns></returns>
    public static Boolean SetIpSecurityPropertySingle(String sMetabasePath, String sMethodName, String sMethodArgument, String sMember, String item, out List<EntityIpDomain> listCurrent, out List<EntityIpDomain> aListNew)
    {
        aListNew = null;

        DirectoryEntry directoryEntry = new DirectoryEntry(sMetabasePath);
        directoryEntry.RefreshCache();

        _oIpSecurity = directoryEntry.Invoke(sMethodName, new[] { sMethodArgument });
        _typeIpSecurityType = _oIpSecurity.GetType();

        //retrieve array of IP(s)|Domain(s)
        Array aDataCurrent = GetIpSecurityData(_oIpSecurity, _typeIpSecurityType, sMember);

        //log entry
        Boolean bExists = ListIpDomainAndCheckIfNewExists(aDataCurrent, item);

        //convert array to list
        listCurrent = ConvertArrayToList(aDataCurrent);

        if (!bExists)
        {
            //instantiate new instance of object dataCurrent
            Object[] oNewData = new object[aDataCurrent.Length + 1];

            //copy dataCurrent into newData
            aDataCurrent.CopyTo(oNewData, 0);

            //add the new value to the newData object
            oNewData.SetValue(item, aDataCurrent.Length);

            //invokes the specified sMember using the arguments supplied
            _typeIpSecurityType.InvokeMember(sMember, BindingFlags.SetProperty, null, _oIpSecurity, new object[] { oNewData });

            //invokes the arguments of the method
            directoryEntry.Invoke("Put", new[] { sMethodArgument, _oIpSecurity });

            //commits the changes
            directoryEntry.CommitChanges();

            //refreshes the cache
            directoryEntry.RefreshCache();

            //return the new list of IP(s)|Domain(s)
            _oIpSecurity = directoryEntry.Invoke("Get", new[] { sMethodArgument });
            Array aDataNew = (Array)_typeIpSecurityType.InvokeMember(sMember, BindingFlags.GetProperty, null, _oIpSecurity, null);

            //log entry
            bExists = ListIpDomainAndCheckIfNewExists(aDataNew, item);

            aListNew = ConvertArrayToList(aDataNew);
        }

        return bExists;
    }

    /// <summary>
    /// Set IP(s)|Domain(s) into SMTP Server
    /// This method is used to insert multiple IPs or DNS entries into the Relay Restriction List in IIS
    /// </summary>
    /// <param name="sMetabasePath">IIS://localhost/smtpsvc/1</param>
    /// <param name="sMethodName">Get|Put</param>
    /// <param name="sMethodArgument">IPSecurity|RelayIPList</param>
    /// <param name="sMember">IPGrant|IPDeny|DomainGrant|DomainDeny</param>
    /// <param name="list">List of IP(s)\Domain(s)</param>
    public static Boolean SetIpSecurityPropertyArray(String sMetabasePath, String sMethodName, String sMethodArgument, String sMember, List<EntityIpDomain> list)
    {
        try
        {
            DirectoryEntry directoryEntry = new DirectoryEntry(sMetabasePath);

            directoryEntry.RefreshCache();

            //return result of Invoke method
            _oIpSecurity = directoryEntry.Invoke(sMethodName, new[] { sMethodArgument });

            //get Type of ipSecurity
            Type typeIpSecurityType = _oIpSecurity.GetType();

            Object[] newList = new object[list.Count];

            Int32 iCounter = 0;
            foreach (EntityIpDomain item in list)
            {
                newList[iCounter] = item.IpDomain;
                iCounter++;
            }

            // add the updated list back to the IPSecurity object
            typeIpSecurityType.InvokeMember(sMember, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty, null, _oIpSecurity, new object[] { newList });

            directoryEntry.Properties[sMethodArgument][0] = _oIpSecurity;

            // commit the changes
            directoryEntry.CommitChanges();
            directoryEntry.RefreshCache();

            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

    /// <summary>
    /// Retrieves the IP(s)|Domain(s) from the SMTP Server in an Array
    /// This method retrieves teh IPs/DNS entries from the Relay Restriction List in IIS
    /// </summary>
    /// <param name="sMetabasePath">IIS://localhost/smtpsvc/1</param>
    /// <param name="sMethodName">Get|Put</param>
    /// <param name="sMethodArgument">IPSecurity|RelayIPList</param>
    /// <param name="sMember">IPGrant|IPDeny|DomainGrant|DomainDeny</param>
    /// <returns></returns>
    public static List<EntityIpDomain> GetIpSecurityPropertyArray(String sMetabasePath, String sMethodName, String sMethodArgument, String sMember)
    {
        //instantiates an instance of DirectoryEntry
        DirectoryEntry directoryEntry = new DirectoryEntry(sMetabasePath);
        directoryEntry.RefreshCache();

        //return result of Invoke method
        object oIpSecurity = directoryEntry.Invoke(sMethodName, new[] { sMethodArgument });

        //get Type of ipSecurity
        Type typeIpSecurityType = oIpSecurity.GetType();

        //returns an array of IPS or Domains
        Array data = GetIpSecurityData(oIpSecurity, typeIpSecurityType, sMember);

        //load the array into a generic list
        List<EntityIpDomain> list = new List<EntityIpDomain>();

        for (int i = 0; i < data.Length; i++)
        {
            EntityIpDomain entityIpDomain = new EntityIpDomain();
            entityIpDomain.IpDomain = data.GetValue(i).ToString();
            list.Add(entityIpDomain);
        }

        return list;
    }

    /// <summary>
    /// Retrieves a list of IPs or Domains
    /// //This is a helper method that actually returns an array of IPs/DNS entries from the Relay Restricton List in IIS
    /// </summary>
    /// <param name="oIpSecurity">Result of directoryEntry.Invoke</param>
    /// <param name="tIpSecurityType">Type of oIpSecurity</param>
    /// <param name="sMember">IPGrant|IPDeny|DomainGrant|DomainDeny</param>
    /// <returns>Array of IP(s)|Domain(s)</returns>
    private static Array GetIpSecurityData(object oIpSecurity, Type tIpSecurityType, String sMember)
    {
        return (Array)tIpSecurityType.InvokeMember(sMember, BindingFlags.GetProperty, null, oIpSecurity, null);
    }

    /// <summary>
    /// Lists the IP(s)|Domain(s)
    /// </summary>
    /// <param name="aData">Array of IP(s)|Domain(s)</param>
    /// <param name="sItem"></param>
    /// <returns>Stringbuilder of the list</returns>
    private static Boolean ListIpDomainAndCheckIfNewExists(Array aData, String sItem)
    {
        Boolean bExists = false;

        StringBuilder stringBuilder = new StringBuilder();

        foreach (object oDataItem in aData)
        {
            stringBuilder.Append(oDataItem + Environment.NewLine);
            if (oDataItem.ToString().StartsWith(sItem))
            {
                bExists = true;
            }
        }

        return bExists;
    }

    /// <summary>
    /// Converts an array to a Genreic List of Type EntityIpDomain
    /// This method converts the array to a list so I can pass it back in a WCF service
    /// </summary>
    /// <param name="aData">Array of IP(s)|Domain(s)</param>
    /// <returns>Generic List of Type EntityIpDomain</returns>
    private static List<EntityIpDomain> ConvertArrayToList(Array aData)
    {
        List<EntityIpDomain> list = new List<EntityIpDomain>(aData.Length);
        foreach (String item in aData)
        {
            EntityIpDomain ipDomainValue = new EntityIpDomain { IpDomain = item };
            list.Add(ipDomainValue);
        }

        return list;
    }
}

}


using System;
using System.Runtime.Serialization;

namespace Ucf.Smtp.Wcf.Entities
{

[DataContract]
public class EntityIpDomain
{
    /// <summary>
    /// Stores the value of the IP|Domain in a String
    /// </summary>
    [DataMember]
    public String IpDomain { get; set; }
}

}
使用系统;
使用System.Collections.Generic;
使用System.DirectoryServices;
使用系统文本;
使用Ucf.Smtp.Wcf.Entities;
运用系统反思;
命名空间Ucf.Smtp.Wcf.BusinessLogic
{
公共静态II类集成
{
私有静态对象_oIpSecurity;
私有静态类型_typeIpSecurityType;
/// 
///将IP |域设置为SMTP服务器
///此方法用于将一个IP或DNS条目插入IIS中的中继限制列表
/// 
///IIS://localhost/smtpsvc/1
///放
///IPSecurity | relayplist
///IPGrant | IPDeny | DomainGrant | DomainDeny
///IP |域
///当前IP|域列表
///新IP域名列表
/// 
公共静态布尔SetIpSecurityPropertySingle(字符串sMetabasePath、字符串sMethodName、字符串sMethodArgument、字符串sMember、字符串项、输出列表listCurrent、输出列表ListNew)
{
aListNew=null;
DirectoryEntry DirectoryEntry=新的DirectoryEntry(sMetabasePath);
directoryEntry.RefreshCache();
_oIpSecurity=directoryEntry.Invoke(sMethodName,new[]{sMethodArgument});
_typeIpSecurityType=\u oIpSecurity.GetType();
//检索IP|域的数组
数组aDataCurrent=GetIpSecurityData(_oIpSecurity,_typeIpSecurityType,sMember);
//日志条目
布尔bExists=ListIpDomainAndCheckIfNewExists(adacCurrent,item);
//将数组转换为列表
listCurrent=ConvertArrayToList(aDataCurrent);
如果(!bExists)
{
//实例化对象dataCurrent的新实例
Object[]oNewData=新对象[aDataCurrent.Length+1];
//将当前数据复制到新数据中
aDataCurrent.CopyTo(oNewData,0);
//将新值添加到newData对象
oNewData.SetValue(项,aDataCurrent.Length);
//使用提供的参数调用指定的sMember
_typeIpSecurityType.InvokeMber(sMember,BindingFlags.SetProperty,null,_oIpSecurity,新对象[]{oNewData});
//调用方法的参数
Invoke(“Put”,new[]{sMethodArgument,_oIpSecurity});
//提交更改
directoryEntry.CommitChanges();
//刷新缓存
directoryEntry.RefreshCache();
//返回IP|域的新列表
_oIpSecurity=directoryEntry.Invoke(“Get”,new[]{sMethodArgument});
数组aDataNew=(数组)_typeIpSecurityType.invokeMber(sMember,BindingFlags.GetProperty,null,_oIpSecurity,null);
//日志条目
bExists=ListIpDomainAndCheckIfNewExists(aDataNew,item);
aListNew=convertrarytolist(aDataNew);
}
回归存在;
}
/// 
///将IP|域设置为SMTP服务器
///此方法用于将多个IP或DNS条目插入IIS中的中继限制列表
/// 
///IIS://localhost/smtpsvc/1
///放
///IPSecurity | relayplist
///IPGrant | IPDeny | DomainGrant | DomainDeny
///IP \域的列表
公共静态布尔SetIpSecurityPropertyArray(字符串sMetabasePath、字符串sMethodName、字符串sMethodArgument、字符串sMember、列表)
{
尝试
{
DirectoryEntry DirectoryEntry=新的DirectoryEntry(sMetabasePath);
directoryEntry.RefreshCache();
//调用方法的返回结果
_oIpSecurity=directoryEntry.Invoke(sMethodName,new[]{sMethodArgument});
//获取ipSecurity的类型
Type typeIpSecurityType=\u oIpSecurity.GetType();
Object[]newList=新对象[list.Count];
Int32 iCounter=0;
foreach(列表中的EntityIpDomain项)
{
newList[iCounter]=item.IpDomain;
iCounter++;
}
//将更新后的列表添加回IPSecurity对象
typeIpSecurityType.InvokeMber(sMember,BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty实例| BindingFlags.SetProperty,null,_oIpSecurity,新对象[]{newList});
directoryEntry.Properties[sMethodArgument][0]=\u oIpSecurity;
//提交更改
directoryEntry.CommitChanges();
directoryEntry.RefreshCache();
返回true;
}
捕获(例外)
{
返回false;
}
}
/// 
///从SMTP服务检索IP|域