以编程方式检查页面是否需要基于web.config设置进行身份验证

Rus*_*sby 4 c# asp.net forms-authentication web-config

我想知道是否有办法检查页面是否需要基于web.config设置进行身份验证.基本上如果有这样的节点

  <location path="account">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>
Run Code Online (Sandbox Code Playgroud)

然后我想检查任何页面是否需要身份验证,如果它在帐户目录下,则返回true.这可能吗?

Rus*_*sby 7

解决方案是创建匿名身份(主体),并将其传递给CheckUrlAccessForPrincipal方法.它将确定页面是公开的还是需要身份验证.

见下面的代码:

var principal = new GenericPrincipal(new GenericIdentity(String.Empty, String.Empty), new string[]{});
bool requiredAuthentication = UrlAuthorizationModule.CheckUrlAccessForPrincipal(Page.AppRelativeVirtualPath, principal, Request.HttpMethod);
Run Code Online (Sandbox Code Playgroud)