Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 确定当前页面是否需要授权?_Asp.net_Forms Authentication - Fatal编程技术网

Asp.net 确定当前页面是否需要授权?

Asp.net 确定当前页面是否需要授权?,asp.net,forms-authentication,Asp.net,Forms Authentication,因此,我有web.configs的web应用程序,如下所示: <authorization> <deny users="?"/> </authorization> ... <location path="SomeUnsecuredPage.aspx"> <system.web> <authorization> <allow users="*"/> </authorizat

因此,我有web.configs的web应用程序,如下所示:

<authorization>
  <deny users="?"/>
</authorization>
...
<location path="SomeUnsecuredPage.aspx">
  <system.web>
    <authorization>
      <allow users="*"/>
    </authorization>
  </system.web>
</location>

...
换句话说,大多数页面需要身份验证和授权,但有些页面不需要

然后我有一个IHttpModule,它将被所有不同的应用程序使用。我要做的就是检查当前请求是否“安全”。如果页面不需要授权,我不希望我的IHttpModule做任何事情。我正在使用FormsAuthentication,我假设FormsAuthentication已经在某个地方缓存了所有这些信息,不是吗?此外,由于该检查将持续运行,因此必须非常快速

我目前正在订阅HttpApplication.AuthorizeRequest,但令人惊讶的是,即使对于允许匿名访问的资源,也会触发此事件


有什么想法吗?谢谢你的阅读

我认为,若服务器返回带有401个未授权状态代码的响应,则资源可能需要授权。但有时服务器可能会重定向到登录页面,所以这种方法不是很可靠。

derp

HttpContext.Current.SkipAuthorization

创建一个盗版IPrincipal,然后你必须使用它。如果盗录主体具有访问权限,则允许匿名访问

public static class AnonymousAccessCheck
            {
                public static bool IsAnonymousAccessAllowed(HttpRequest request)
                {
                    // unfortunately checking if a page allows anonymous access is more complicated than you'd think(I think).
                    // here we have to create a "Fake" IPrincipal that will only ever have access to 
                    // pages that allow anonymous access.  That way if our fake principal has access,
                    // then anonymous access is allowed

                    UrlAuthorizationModule urlAuthorizationModule = new UrlAuthorizationModule();
                    return UrlAuthorizationModule.CheckUrlAccessForPrincipal(request.Path, AnonymousPrincipal.Instance, request.RequestType);
                }

                private class AnonymousPrincipal : IPrincipal
                {
                    private static AnonymousPrincipal _Instance;
                    public static AnonymousPrincipal Instance
                    {
                        get
                        {
                            if (_Instance == null)
                                _Instance = new AnonymousPrincipal();

                            return _Instance; 
                        }
                    }

                    private AnonymousPrincipal()
                    {
                        _Identity = new AnonymousIdentity();
                    }

                    private readonly IIdentity _Identity;

                    #region IPrincipal Members

                    public IIdentity Identity
                    {
                        get { return _Identity; }
                    }

                    public bool IsInRole(string role)
                    {
                        return false;
                    }

                    #endregion

                    private class AnonymousIdentity : IIdentity
                    {
                        #region IIdentity Members
                        public string AuthenticationType
                        {
                            get { return string.Empty; }
                        }

                        public bool IsAuthenticated
                        {
                            get { return false; }
                        }

                        public string Name
                        {
                            get { return string.Empty; }
                        }
                        #endregion
                    }
                }
            }

更直接的方法如下:

var method = typeof(UrlAuthorizationModule).GetMethod("RequestRequiresAuthorization", BindingFlags.NonPublic | BindingFlags.Static);
var requiresAuthentication = (Boolean)method.Invoke(null, new object[] { HttpContext.Current });
使用此选项之前,请确保您的网站具有执行反射的权限

我一直不明白为什么微软隐藏了这么多使用“内部”的API(就像这个方法)。在我看来,如果微软必须在内部公开一些东西,那么很可能有人,也会有人需要它


您可以使用通用标识,而不是创建盗版主体/标识

public bool IsAnonymousAccessAllowed()
{
   return UrlAuthorizationModule.CheckUrlAccessForPrincipal(Request.Path, new GenericPrincipal(new GenericIdentity(""), new string[0]), Request.RequestType);
}

事实上,没关系。对于web.config中设置的“我的授权排除”,这始终为false。只有Login.aspx和WebResource.axd这样的页面才将此属性设置为true。否决我创建单例IPrincipal实例的答案+1。非常有用的一段代码。但是,它不需要创建
UrlAuthorizationModule
的实例。只需使用静态方法
CheckUrlAccessForPrincipal
就足够了。