首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >以编程方式将IP或域添加到IIS 6上SMTP虚拟服务器中的中继限制

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

Stack Overflow用户
提问于 2009-12-22 03:27:13
回答 1查看 2.9K关注 0票数 0

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

EN

回答 1

Stack Overflow用户

发布于 2010-05-11 19:51:04

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

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

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1942074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档