RE: Problem setting authentication modes on files under a virtual directory

From: Ram Parab[MS] (rparab@online.microsoft.com)
Date: 11/01/02


From: rparab@online.microsoft.com ("Ram Parab[MS]")
Date: Fri, 01 Nov 2002 19:07:10 GMT


Hello Joel,

Here is the code that you should take care of this:

                public bool IsExists(string path, string filename)
                {
                        DirectoryEntry directoryEntry = new DirectoryEntry(String.Concat(path,
"/", filename));
                        try
                        {
                                string name = directoryEntry.Name;
                                return true;
                        }
                        catch
                        {
                                return false;
                        }
                }

                //changePermOnFileInVDir("IIS://Localhost/W3SVC/1/Root/ASP", "Test.asp");
                public void changePermOnFileInVDir(string path, string file)
                {
                        DirectoryEntry de;
                        if(IsExists(path, file))
                        {
                                de = new DirectoryEntry(path+ "/" + file);
                        }
                        else
                        {
                                //add new entry here
                                DirectoryEntry vdir = new DirectoryEntry(path);
                                DirectoryEntries des = vdir.Children;
                                de = des.Add(file, "IIsWebFile" );
                        }
                        de.Properties["AuthNTLM"][0] = true;
                        de.Properties["AuthAnonymous"][0]= false;
                        de.CommitChanges();
                }

The reason you see this problem is due to the fact that IIS metabase does
not have an entry for the file in the virtual directory for which you are
trying to set the auth properties. You can verify this by changing security
setting of a file in vdir and then run your application, now you would not
see the error. You can also use metaedit.exe to peak into IIS metabase.

Thank you,
Ram Parab
Microsoft Developer Support

This posting is provided "AS IS", with no warranties, and confers no rights.