3

最近我正在实现一个 HttpMoudle。并坚持所说的错误System.NullReferenceException: Object reference not set to an instance of an object.

这是我的代码。

public class MyHttpModuler : IHttpModule
    {
        private static IAuthProvider authProvider=null;
        #region IHttpModule members
        /// <summary>
        /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
        /// </summary>
        public void Dispose()
        {
            authProvider.Dispose();
            authProvider = null;
        }

        public void Init(HttpApplication application)
        {
            authProvider = new BasicAuthProvider("achievo");
            application.BeginRequest += new EventHandler(Application_BeginRequest);

        }

        private void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            HttpRequest request = app.Request;
            HttpResponse response = app.Response;
            TryAuthenticate(app);                    
        }
        #endregion


        #region private method
        /// <summary>
        /// Tries to authenticate the user
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        private bool TryAuthenticate(HttpApplication context)
        {
            string authHeader = context.Request.Headers["Authorization"];
            if (!string.IsNullOrEmpty(authHeader))
            {
                if (authHeader.StartsWith("basic ", StringComparison.InvariantCultureIgnoreCase))
                {

                    string userNameAndPassword = Encoding.Default.GetString(
                        Convert.FromBase64String(authHeader.Substring(6)));
                    string[] parts = userNameAndPassword.Split(':');
                     if (authProvider.IsValidUser(parts[0], parts[1]))
                    {
                        //the authProvider object sometimes is null .Why?                            
                        return true;
                    }
                }
            }
            return false;
        }
        #endregion
    }

public class BasicAuthProvider : IAuthProvider
    {

        #region IAuthProvider Members

        public string DomainName { get; set; }

        public BasicAuthProvider(string sDmName)
        {
            DomainName = sDmName;
        }

        public bool IsValidUser(string userName, string password)
        {

            string sFullName = "LDAP://" + DomainName + ".com";
            bool bLogin = ADHelper.IsAuthenticated(sFullName, DomainName + @"\" + userName, password);
            return bLogin;
        }


        public bool IsRequestAllowed(HttpRequest request,string sName)
        {
            return sName == "joe.wang";
        }



        public void Dispose()
        {

        }

        #endregion
    }

尤其是当多个用户进入网站时。发生 NullReferenceException 异常。当我调试时,我发现有时Init方法可能不会被调用。也许这就是发生异常的原因。任何人都可以帮我检查一下吗?谢谢

4

2 回答 2

2

您已将主要对象设置为静态authProvider,因此您需要在制作和删除它时进行同步。

private static IAuthProvider authProvider=null;

所以你需要把它写成:

public class MyHttpModuler : IHttpModule
    {
        //can not be shared with others request.
        //private IAuthProvider authProvider = null;

        //We do not need it now.
        //private static readonly object _lockObj = new object();

        #region IHttpModule members
        /// <summary>
        /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
        /// </summary>
        public void Dispose()
        {
            //authProvider.Dispose();
        //authProvider = null;
        }

        public void Init(HttpApplication application)
        { 

            application.BeginRequest += new EventHandler(Application_BeginRequest);       
        }

        private void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            HttpRequest request = app.Request;
            HttpResponse response = app.Response;
            TryAuthenticate(app);                    
        }
        #endregion


        #region private method
        /// <summary>
        /// Tries to authenticate the user
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        private bool TryAuthenticate(HttpApplication context)
        {
            IAuthProvider authProvider = new BasicAuthProvider("achievo");  
            string authHeader = context.Request.Headers["Authorization"];
            if (!string.IsNullOrEmpty(authHeader))
            {
                if (authHeader.StartsWith("basic ", StringComparison.InvariantCultureIgnoreCase))
                {

                    string userNameAndPassword = Encoding.Default.GetString(
                        Convert.FromBase64String(authHeader.Substring(6)));
                    string[] parts = userNameAndPassword.Split(':');
                     if (authProvider.IsValidUser(parts[0], parts[1]))
                    {
                        //the authProvider object sometimes is null .Why?     
                        authProvider=null;                        
                        return true;
                    }
                }
            }
            authProvider=null;  
            return false;
        }
        #endregion
    }
于 2012-08-22T09:25:51.673 回答
2

问题是(正如Aristos指出的那样)您已经制作了authProvider静态文件。

这意味着当多个用户同时生成请求时,将会有多个您的实例HttpModule同时运行。但他们都会共享一个 authProvider

因此,用户 A可能会启动一个请求,从而导致您的模块的一个新实例运行它的Init方法。然后处理该请求的线程被搁置,来自用户 B的另一个请求开始,导致另一个新的模块实例运行它的Init方法。这将覆盖在用户 A的请求中放置的 authProvider 中的实例。然后线程被搁置,来自用户 A的前一个线程被恢复并完成处理请求,从而处置authProvider(由用户 B的请求设置)并将其设置为空。之后,来自用户 B 的请求再次恢复并发现authProvider现在为空。

根据您提供的代码,似乎最简单的解决方案是简单地static从行中删除关键字:

private static IAuthProvider authProvider=null;

然后每个实例都HttpModule将拥有它自己的副本,authProvider并且一个请求不会影响另一个请求的状态。

于 2012-08-22T10:12:25.940 回答