views:

44

answers:

1

I'm currently using System.DirectoryServices.DirectoryEntry and the 'AuthFlags' property therein to set Anonymous access to a virtual web. To enable anonymous access I give it a value of 1. What value do I need to set to enable forms auth?

I have this idea in the back of my head that maybe this is only set via the web.config?

A: 

I notice you're using System.DirectoryServices to configure these features on IIS7 (according to your tags).

In IIS7 you can configure both of these settings using the Microsoft.Web.Administration library instead:

Setting the authentication type (replaces AuthFlags):

IIS 7 Configuration: Security Authentication <authentication>

To configure Forms Authentication:

using Microsoft.Web.Administration;
   ...
long iisNumber = 1234;
using(ServerManager serverManager = new ServerManager())
{
  Site site = serverManager.Sites.Where(s => s.Id == iisNumber).Single();

  Configuration config = serverManager.GetWebConfiguration(site.Name);
  ConfigurationSection authenticationSection = 
               config.GetSection("system.web/authentication");
  authenticationSection.SetAttributeValue("mode", "Forms");

  ConfigurationSection authorizationSection = 
               config.GetSection("system.web/authorization");
  ConfigurationElementCollection addOrDenyCollection = 
               authorizationSection.GetCollection();
  ConfigurationElement allowElement = addOrDenyCollection.CreateElement("allow");
  allowElement["users"] = "?";

  addOrDenyCollection.Add(allowElement);
  serverManager.CommitChanges();
}

The code above will create a new web.config file in the root of the website or modify an existing one.

To use Microsoft.Web.Administration, add a reference to C:\Windows\System32\InetSrv\Microsoft.Web.Administration.dll.

Kev
So kinda like I was saying in my last sentence, you're saying the best way is to just modify the web.config? Thanks for this btw.
ZIP Code Database
@zip - yes that would be the way to do this.
Kev
@zip - did this answer help you, if it did can you mark as correct and upvote?
Kev