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

时间:2009-12-21 19:27:13

标签: c# iis smtp

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

alt text

1 个答案:

答案 0 :(得分:2)

经过大量研究试图找到在IIS中访问中继限制列表的最佳方法后,我想出了这个类。这非常适合我们尝试的操作,因此请随意使用代码并使其适应您的使用。我送给StackOverflow的礼物是每天给我很好的答案。

我发现这个解决方案是解决问题的最简单,最干净的方法。我使用它在IIS中的中继限制列表中插入和检索IP和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; }
}

}