以编程方式在 IIS 7.0 中启用表单身份验证 [英] Programmatically enable forms authentication in IIS 7.0

查看:36
本文介绍了以编程方式在 IIS 7.0 中启用表单身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 System.DirectoryServices.DirectoryEntry 和其中的AuthFlags"属性来设置对虚拟网络的匿名访问.为了启用匿名访问,我给它一个值 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?

我有一个想法,也许这只能通过 web.config 设置?

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

推荐答案

我注意到您正在使用 System.DirectoryServices 在 IIS7 上配置这些功能(根据您的标签).

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

在 IIS7 中,您可以使用 Microsoft.Web.Administration 库来配置这两个设置:

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

设置认证类型(代替AuthFlags):

Setting the authentication type (replaces AuthFlags):

IIS 7 配置:安全认证<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();
}

上面的代码会在网站根目录下创建一个新的web.config文件或者修改一个已有的.

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

要使用 Microsoft.Web.Administration,请添加对 C:WindowsSystem32InetSrvMicrosoft.Web.Administration.dll 的引用.

To use Microsoft.Web.Administration, add a reference to C:WindowsSystem32InetSrvMicrosoft.Web.Administration.dll.

这篇关于以编程方式在 IIS 7.0 中启用表单身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆