使用Forms认证与location配置权限时遇authentication配置节报错求助
问题原因与解决方案
错误原因
<authentication>是站点级的配置节,只能在根目录的web.config中定义,子目录的web.config里不能包含这个节点,这就是触发“配置节'authentication'无法读取,因为缺少节声明”错误的核心原因。
修正步骤
迁移认证配置到根目录web.config
把Forms认证的配置移到网站根目录的web.config中,示例如下:<configuration> <system.web> <authentication mode="Forms"> <forms name=".ASPXF2" loginUrl="~/LoggedIn/LoginPage.aspx" protection="All" path="/" timeout="30" /> </authentication> <!-- 根目录默认允许所有用户访问,可根据需求调整 --> <authorization> <allow users="*" /> </authorization> </system.web> </configuration>修改子目录LoggedIn的web.config
删除子目录web.config中的<authentication>节点,只保留授权规则配置即可。如果仅需限制Default_USER.aspx页面,配置如下:<configuration> <location path="Default_USER.aspx"> <system.web> <authorization> <deny users="?" /> </authorization> </system.web> </location> </configuration>如果要限制整个
LoggedIn目录下的所有页面,可简化为:<configuration> <system.web> <authorization> <deny users="?" /> </authorization> </system.web> </configuration>
补充说明
子目录的web.config仅用于覆盖根目录的授权规则,站点级的认证配置必须统一放在根目录,这样就能正常实现“仅已登录/注册用户访问特定页面”的需求。
内容的提问来源于stack exchange,提问作者Laurent
相关产品推荐
相关产品推荐

