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

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

问题描述

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

推荐答案

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

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

在IIS7中,您可以使用<$ c配置这两个设置$ c> 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>

使用Microsoft.Web.Administration配置表单身份验证:

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();
}

上面的代码将创建一个新的 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:\ Windows \ System32 \ InetSrv \ Microsoft.Web.Administration.dll

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

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