⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpmodule.cs

📁 ASP.NET简洁论坛源代码 这是一个简单的论坛
💻 CS
字号:
using System;
using System.IO;
using System.Reflection;
using System.Web;
using System.Text.RegularExpressions;
using NetFocus.Web.Core;

namespace NetFocus.Web.Applications.Forum 
{
    public class HttpModule : IHttpModule
    {
        #region Implementation of IHttpModule

        public void Init(HttpApplication application) 
        {
            application.BeginRequest += new EventHandler(this.Application_BeginRequest);
            application.AuthenticateRequest += new EventHandler(Application_AuthenticateRequest);
            application.Error += new EventHandler(this.Application_OnError);
            application.AuthorizeRequest += new EventHandler(this.Application_AuthorizeRequest);
            application.EndRequest += new EventHandler(this.Application_EndRequest);
        }
        public void Dispose() 
        {
        }

        #endregion

        private void Application_OnError (Object source, EventArgs e) 
        {
            //TODO
        }
        private void Application_AuthenticateRequest(Object source, EventArgs e) 
        {
            if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith(".ashx"))
            {
                return;
            }
            SetCurrentUser();
        }
        private void Application_AuthorizeRequest (Object source, EventArgs e)
        {
            if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith(".ashx"))
            {
                return;
            }
            WebContext context = WebContext.Current;
            if (context.HttpContext.Request.IsAuthenticated)
            {
                WebContext.Current.User.SetRoles(Roles.GetUserRoles(WebContext.Current.User.UserId));
            }
        }
		private void Application_BeginRequest(Object source, EventArgs e) 
		{
            if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith(".ashx"))
            {
                return;
            }
            WebContext.Create(HttpContext.Current, new UrlReWriterHandler(ReWriteUrl));
		}  
        private void Application_EndRequest (Object source, EventArgs e) 
        {
            if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith(".ashx"))
            {
                return;
            }
            //TODO
        }

        private bool ReWriteUrl(HttpContext context)
        {
            UrlReWriter urlProvider = UrlReWriter.Instance();
            string path = context.Request.Path;
            string newPath = urlProvider.GetRewrittenUrl(path, context.Request.Url.Query);
            if (newPath != null)
            {
                string qs = null;
                int index = newPath.IndexOf('?');
                if (index >= 0)
                {
                    qs = (index < (newPath.Length - 1)) ? newPath.Substring(index + 1) : string.Empty;
                    newPath = newPath.Substring(0, index);
                }
                urlProvider.RewriteUrl(context, newPath, qs);
            }

            return newPath != null;
        }
        private void SetCurrentUser()
        {
            if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)
            {
                WebContext.Current.User = ForumUsers.GetUser(HttpContext.Current.User.Identity.Name);
                if (WebContext.Current.User == null)
                {
                    WebContext.Current.User = ForumUsers.GetAnonymousUser();
                }
            }
            else
            {
                WebContext.Current.User = ForumUsers.GetAnonymousUser();
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -