📄 communitiesmodule.cs
字号:
namespace ASPNET.StarterKit.Communities {
using System;
using System.Web;
using System.Globalization;
using System.Threading;
using System.IO;
//*********************************************************************
//
// CommunitiesModule Class
//
// This HTTP module translates all requests for pages
// into requests for the community default page. It also initializes
// CommunityInfo, SectionInfo, UserInfo and (optionally) PageInfo objects for
// each request and places these objects into the Context.
//
//*********************************************************************
public class CommunitiesModule : IHttpModule {
//*********************************************************************
//
// Init Method
//
// Associates handlers with the BeginRequest and AuthenticateRequest
// events.
//
//*********************************************************************
public void Init(HttpApplication application) {
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
application.AuthenticateRequest += (new EventHandler(this.Application_AuthenticateRequest));
}
//*********************************************************************
//
// Application_BeginRequest Method
//
// Determines the Community, Section, and Page associated
// with each request.
//
//*********************************************************************
private void Application_BeginRequest(Object source, EventArgs e) {
// Get abbreviated reference to current context
HttpContext Context = HttpContext.Current;
// set culture based on acceptlanguage
if (Context.Request.UserLanguages != null) {
try {
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Context.Request.UserLanguages[0]);
} catch {}
}
// Retrieve path
string requestPath = CommunityGlobals.RemovePathInfo(Context.Request.Path.ToLower());
string requestBasePath = CommunityGlobals.GetSectionPath(requestPath);
// Figure out the community and add to Context
CommunityInfo objCommunityInfo = CommunityUtility.GetCommunityInfo();
if (objCommunityInfo == null)
return;
Context.Items[ "CommunityInfo" ] = objCommunityInfo;
// Figure out the section and add to context
SectionInfo objSectionInfo = SectionUtility.GetSectionInfoFromPath(requestBasePath);
if (objSectionInfo == null)
return;
Context.Items[ "SectionInfo" ] = objSectionInfo;
// Is this a Web Service request?
if (requestPath.EndsWith(".asmx")) {
Context.RewritePath(CommunityGlobals.UrlBaseService + Context.Request.PathInfo);
return;
}
// Figure out the page and add to Context
if (requestPath.EndsWith(".aspx")) {
PageInfo objPageInfo = GetPageInfo(objSectionInfo, requestPath);
if (objPageInfo != null) {
Context.Items[ "PageInfo" ] = objPageInfo;
Context.RewritePath(CommunityGlobals.UrlBasePage);
}
}
}
//*********************************************************************
//
// GetPageInfo Method
//
// Retrieves the PageInfo associated with the page begin
// being requested.
//
//*********************************************************************
private PageInfo GetPageInfo(SectionInfo objSectionInfo, string requestPath) {
// Get Request Parts
string requestFile = CommunityGlobals.GetPageName(requestPath);
PageInfo objPageInfo = null;
// Check for section default page
if (requestFile.ToLower() == "/default.aspx")
return new PageInfo
(
-1,
objSectionInfo.Name,
objSectionInfo.ParentSectionID,
objSectionInfo.PageType,
objSectionInfo.Title,
objSectionInfo.Description,
objSectionInfo.PageMetaDesc,
objSectionInfo.PageMetaKeys,
objSectionInfo.Content
);
// Check for named page
objPageInfo = NamedPageUtility.GetPageInfoFromNamedPage(requestFile);
if (objPageInfo != null)
return objPageInfo;
// Check for content page
objPageInfo = ContentPageUtility.GetPageInfoFromPageContentID(requestFile, objSectionInfo);
if (objPageInfo != null)
return objPageInfo;
// If everything fails, just return null
return null;
}
//*********************************************************************
//
// Application_AuthenticateRequest Event
//
// If the client is authenticated with the application, then determine
// which security roles he/she belongs to and replace the "User" intrinsic
// with a custom IPrincipal security object that permits "User.IsInRole"
// role checks within the application
//
// Roles are cached in the browser in an in-memory encrypted cookie. If the
// cookie doesn't exist yet for this session, create it.
//
//*********************************************************************
void Application_AuthenticateRequest(Object sender, EventArgs e) {
HttpContext Context = HttpContext.Current;
// Get the user roles
UserUtility.GetUserRoles();
// Get SectionInfo
SectionInfo objSectionInfo = (SectionInfo)Context.Items[ "SectionInfo" ];
if (objSectionInfo == null)
return;
// Create the User Info Object
UserInfo objUserInfo = new UserInfo(objSectionInfo);
Context.Items[ "UserInfo" ] = objUserInfo;
// Make sure the user can view the page
if (!objUserInfo.MayView)
CommunityGlobals.ForceLogin();
}
//*********************************************************************
//
// Dispose Method
//
// This method is required by the HttpModule Interface.
//
//*********************************************************************
public void Dispose() {}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -