C#下编程完成IIS网络App的权限设置

转自:http://linwx1978.blog.163.com/blog/static/1504106920101104834271/

以前的日志中转了不少文章,最近听说转文不是好习惯,决定普世一把,改贴链接了。大家有兴趣的话可以顺着链接进去看看:
http://geekswithblogs.net/mnf/articles/78888.aspx
用途是,对于IIS下的网络应用程序,通过编程改变权限设置。IIS Manager当然挺方便的,但是有些时候(特别是在做部署文件的时候)是不能使用图形界面的,所以必需用编程解决。文中的代码我测试了一下,还是挺方便的,但有几个地方要修改:
1、把DebugHelper的行注掉。(DebugHelper是另外一个作者自己写的类)
2、工程中增加对System.DirectoryServices的引用。
3、工程中用到了StringHelper,不知道是什么东西,不过有趣的是在用到的地方后面注解里提供了一个等价的实现,只要释放出来就可以了(删除黄色的部分):
string sEntryName = StringHelper.RightAfterLast(MetabasePath, @"/");//metabasePath.Substring(metabasePath.LastIndexOf("/") + 1);
4、IISMetaPath小小地做个修改:
        public static string IISMetaPath(string SiteMetaPath, String VDirRoot, String relPath)
        {
            SiteMetaPath = SiteMetaPath.Replace(@"/LM/", "localhost/");
            return @"IIS://" + SiteMetaPath + @"/root/" + VDirRoot + (relPath != "" ? @"/" : "") + relPath;
        }
大家可以对照原文看一下,修改的地方是红色的部分,原文的程序好像是只支持设置应用程序下的某个路径,修改了之后,只要在relPath中填入空字符串就可以设置应用程序了。

补充:微软发布了一个很有用的工具包叫IIS Resources,其中有个工具叫IIS Metabase Explorer,可以看到网络App几乎所有的设置(没敢把话说满是因为还没有仔细研究过对应关系)。就本文中涉及到的应用而言,其实就是其中的一个键值(AuthFlags)。

有一点要注意,在IIS管理中,在设置Hanlder Mappings的地方我们会看到五个可设置项,其中包括一个ASP.NET Impersonation,但是在http://technet2.microsoft.com/WindowsServer/en/Library/271ae19b-853f-4672-b743-5ba126e902db1033.mspx?mfr=true中虽然也有五个项,却没有一个对应上面说到的这个设置项的……嘿嘿,原来这个选项是在web.config中设置的,位置是:system.web > identity

  • <identity impersonate="false" />关闭
  • <identity impersonate="true" />开放:使用IIS的帐户
  • <identity impersonate="true"  userName="..." password="..."/>使用一个指定的帐户


下面帖上我改造后的代码,谨供分享:(注意后面涉及到了另外一个应用,即对于IIS5,加上了.mvc后缀的handler,目前的代码不知道为什么在IIS7下无效,不过也没有害处)。

        public static string IISMetaPath(string Site, string VDirRoot, string relPath)
        {
            if (relPath != @"/")
            {
                if (!relPath.StartsWith(@"/"))
                {
                    relPath = @"/" + relPath;
                }
                if (relPath.EndsWith(@"/"))
                {
                    relPath = relPath.Substring(0, relPath.Length - 1);
                }
            }
            else
            {
                relPath = "";
            }

            return @"IIS://" + Site + @"/W3SVC/1/root/" + VDirRoot + relPath;
        }

        static void Main(string[] args)
        {
            DirectoryEntry entry = null;

            //Application: disable Anonymous Auth and enable Basic Auth
            string sPath = IISMetaPath(@"localhost", @"……(应用程序名)", @"/");
            entry = new DirectoryEntry(sPath);

            AuthFlags nAuthFlags = (AuthFlags)(entry.Properties[KEY_AUTHFLAGS][0]);
            nAuthFlags = nAuthFlags & ~AuthFlags.AuthAnonymous; //clear anonymous
            nAuthFlags = nAuthFlags | AuthFlags.AuthBasic; //Add Basic 
            entry.Properties[KEY_AUTHFLAGS][0] = nAuthFlags;
            entry.CommitChanges();

            //Check script mapping list
            bool mvcExist = false;
            foreach (string s in entry.Properties[KEY_SCRIPTMAPS])
            {
                char[] splitter = { ',' };
                string[] list = s.Split(splitter);
                if (list[0] == ".mvc")
                {
                    mvcExist = true;
                    break;
                }
            }

            if (!mvcExist)
            {
                entry.Properties[KEY_SCRIPTMAPS].Add(@".mvc,%systemroot%Microsoft.NETFramework" +
                    @"v2.0.50727aspnet_isapi.dll,1");
                entry.CommitChanges();
            }

以下略……

原文地址:https://www.cnblogs.com/duanweishi/p/4273466.html