Transferred from: http://linwx1978.blog.163.com/blog/static/1504106920101104834271/
The previous log transit a lot of articles, recently heard that turn is not a good habit, decided to a universal, changed the link. If you are interested, you can follow the link to see:
Http://geekswithblogs.net/mnf/articles/78888.aspx
The purpose is to programmatically change the permission settings for a network application under IIS. IIS Manager is of course very handy, but sometimes (especially when it comes to deploying files) you can't use a graphical interface, so you need to use programming to solve it. The code in the article I tested, or very convenient, but there are several places to modify:
1, the Debughelper line off. (Debughelper is a class written by another author himself)
2, the project added to the System.DirectoryServices reference.
3, the project used to Stringhelper, do not know what, but it is interesting to use the place behind the annotations provide an equivalent implementation, as long as the release can be (delete the yellow part):
string sentryname = stringhelper. Rightafterlast (MetabasePath, @ "/"); Metabasepath.substring (Metabasepath.lastindexof ("/") + 1);
4, Iismetapath Small to make a change:
public static string Iismetapath (String Sitemetapath, String vdirroot, String relPath)
{
Sitemetapath = Sitemetapath.replace (@ "/lm/", "localhost/");
return @ "iis://" + Sitemetapath + @ "/root/" + Vdirroot + (RelPath! = ""? @ "/": "") + RelPath;
}
You can look at the original text, modify the place is the red part, the original program appears to support only a certain path under the application, modified, as long as the relpath to fill in the empty string can be set up the application.
Add: Microsoft has released a useful toolkit called IIS Resources, which has a tool called IIS Metabase Explorer, and you can see almost all of the settings of the Web app (and don't dare to say that it has not been carefully studied for correspondence). In terms of the application involved in this article, it is actually one of the key values (AuthFlags).
It is important to note that in IIS administration, where you set up hanlder mappings, we see five items that can be set, including an ASP. Impersonation, but in http://technet2.microsoft.com/ Although there are five items in the windowsserver/en/library/271ae19b-853f-4672-b743-5ba126e902db1033.mspx?mfr=true, there is no one corresponding to the above-mentioned setting. Hey, it turns out this option is set in Web. config, location: system.web > Identity
- <identity impersonate= "false"/> Off
- <identity impersonate= "true"/> Open: Account with IIS
- <identity impersonate= "true" username= "..." password= "..."/> Use a specified account
The following post my modified code, for sharing: (note that the following involves another application, that is, for IIS5, plus the. Mvc suffix of the handler, the current code does not know why the IIS7 under the invalid, but there is no harm).
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", @ "... (Application name) ", @"/");
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.net\framework\" +
@ "v2.0.50727\aspnet_isapi.dll,1");
Entry.commitchanges ();
}
The following is slightly ...
C # Programming to complete permissions settings for IIS Web Apps