📄 commonmodule.cs
字号:
namespace PowerEasy.Web.HttpModule
{
using PowerEasy.Components;
using PowerEasy.Enumerations;
using PowerEasy.Model.UserManage;
using PowerEasy.StaticHtml;
using PowerEasy.UserManage;
using PowerEasy.Web;
using System;
using System.Globalization;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
public class CommonModule : IHttpModule
{
private void Application_AuthenticateRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication) sender;
HttpContext context = application.Context;
FormsAuthenticationTicket ticket = null;
string formsCookieName = FormsAuthentication.FormsCookieName;
HttpCookie cookie = context.Request.Cookies[formsCookieName];
if (cookie == null)
{
UserPrincipal principal = new UserPrincipal(new AnonymousAuthenticateIdentity());
principal.UserInfo = new UserInfo(true);
principal.UserInfo.GroupId = -2;
principal.UserInfo.IsInheritGroupRole = true;
PEContext.Current.User = principal;
}
else
{
try
{
ticket = FormsAuthentication.Decrypt(cookie.Value);
}
catch (ArgumentException exception1)
{
if (exception1 != null)
{
return;
}
}
catch (CryptographicException)
{
context.Request.Cookies.Remove(formsCookieName);
}
if (ticket != null)
{
UserPrincipal principal2 = UserPrincipal.CreatePrincipal(ticket);
if (principal2.Identity.IsAuthenticated)
{
principal2.UserInfo = Users.GetUsersByUserName(principal2.UserName);
UserPurviewInfo userPurview = principal2.UserInfo.UserPurview;
principal2.PurviewInfo = userPurview;
PEContext.Current.User = principal2;
FormsIdentity identity = new FormsIdentity(ticket);
GenericPrincipal principal3 = new GenericPrincipal(identity, new string[] { principal2.RoleId.ToString(CultureInfo.CurrentCulture) });
context.User = principal3;
}
else
{
GenericPrincipal principal4 = new GenericPrincipal(new NoAuthenticateIdentity(), null);
context.User = principal4;
}
}
}
}
private static void CheckUserLogin(HttpContext context)
{
bool flag = true;
AuthorizationSection section = (AuthorizationSection) context.GetSection("system.web/authorization");
if (((section.Rules.Count > 0) && (section.Rules[0].Action == AuthorizationRuleAction.Allow)) && section.Rules[0].Users.Contains("*"))
{
flag = false;
}
if (flag && context.Request.Url.GetLeftPart(UriPartial.Path).EndsWith(".aspx", StringComparison.OrdinalIgnoreCase))
{
if (PEContext.Current.User.Identity.IsAuthenticated)
{
bool flag2 = false;
UserInfo userInfo = PEContext.Current.User.UserInfo;
if (userInfo.Status != UserStatus.None)
{
Utility.WriteUserErrMsg(Utility.GetGlobalErrorString("UserIsNotApprove"), "~/Default.aspx");
}
if (!SiteConfig.UserConfig.EnableMultiLogin && (PEContext.Current.User.LastPassword != userInfo.LastPassword))
{
Utility.WriteUserErrMsg(Utility.GetGlobalErrorString("MultiUserLoginSystem"), "~/User/Login.aspx");
}
if (SiteConfig.UserConfig.PresentExpPerLogin > 0.0)
{
bool flag3 = false;
if (!userInfo.LastPresentTime.HasValue)
{
flag3 = true;
}
else
{
TimeSpan span = (TimeSpan) (DateTime.Now - userInfo.LastPresentTime.Value);
if (span.TotalDays >= 1.0)
{
flag3 = true;
}
}
if (flag3)
{
userInfo.UserExp += (int) SiteConfig.UserConfig.PresentExpPerLogin;
userInfo.LastPresentTime = new DateTime?(DateTime.Now);
flag2 = true;
}
}
if ((context.Session != null) && (context.Session["UserName"] == null))
{
userInfo.LoginTimes++;
userInfo.LastLoginTime = new DateTime?(DateTime.Now);
userInfo.LastLoginIP = PEContext.Current.UserHostAddress;
flag2 = true;
context.Session.Add("UserName", PEContext.Current.User.UserName);
}
if (!userInfo.LastLoginTime.HasValue)
{
userInfo.LastLoginTime = new DateTime?(DateTime.Now);
}
if (flag2)
{
Users.Update(userInfo);
}
}
}
else if (PEContext.Current.User.Identity.IsAuthenticated && (PEContext.Current.User.UserInfo.Status != UserStatus.None))
{
UserPrincipal principal = new UserPrincipal(new AnonymousAuthenticateIdentity());
principal.UserInfo = new UserInfo(true);
principal.UserInfo.GroupId = -2;
principal.UserInfo.IsInheritGroupRole = true;
PEContext.Current.User = principal;
GenericPrincipal principal2 = new GenericPrincipal(new NoAuthenticateIdentity(), null);
context.User = principal2;
FormsAuthentication.SignOut();
}
}
private void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication) sender;
CheckUserLogin(application.Context);
}
public void Dispose()
{
Jobs.Instance().Stop();
}
public void Init(HttpApplication context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
AuthenticationSection section = (AuthenticationSection) WebConfigurationManager.GetSection("system.web/authentication");
if (section.Mode == AuthenticationMode.Forms)
{
context.AuthenticateRequest += new EventHandler(this.Application_AuthenticateRequest);
context.PreRequestHandlerExecute += new EventHandler(this.context_PreRequestHandlerExecute);
}
SiteConfigInfo config = SiteConfig.ConfigInfo();
string virtualPath = config.SiteInfo.VirtualPath;
if (string.IsNullOrEmpty(virtualPath) || (string.Compare(virtualPath, VirtualPathUtility.AppendTrailingSlash(HttpContext.Current.Request.ApplicationPath), true, CultureInfo.CurrentCulture) != 0))
{
config.SiteInfo.VirtualPath = HttpContext.Current.Request.ApplicationPath;
new SiteConfig().Update(config);
}
Jobs.Instance().Start();
}
public string ModuleName
{
get
{
return "CommonModule";
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -