101 lines
3.8 KiB
C#
101 lines
3.8 KiB
C#
using System;
|
|
using System.Web.Mvc;
|
|
using System.Web.Security;
|
|
using WX.CRM.WebHelper.UtilityModel;
|
|
|
|
namespace WX.CRM.WebHelper
|
|
{
|
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
|
|
public class AuthorizeRedirect : AuthorizeAttribute
|
|
{
|
|
private const string IS_AUTHORIZED = "isAuthorized";
|
|
|
|
public string RedirectUrl = "~/Base/Account/UnAuthorized";
|
|
|
|
private MenuLogHelper menuLogHelper;
|
|
|
|
public AuthorizeRedirect()
|
|
{
|
|
if (menuLogHelper == null)
|
|
menuLogHelper = new MenuLogHelper();
|
|
}
|
|
|
|
public AuthorizeRedirect(string ActionName)
|
|
{
|
|
this.Roles = ActionName;
|
|
if(menuLogHelper == null)
|
|
menuLogHelper = new MenuLogHelper();
|
|
}
|
|
|
|
protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
|
|
{
|
|
bool isAuthorized = base.AuthorizeCore(httpContext);
|
|
|
|
httpContext.Items.Add(IS_AUTHORIZED, isAuthorized);
|
|
|
|
return isAuthorized;
|
|
}
|
|
|
|
public override void OnAuthorization(AuthorizationContext filterContext)
|
|
{
|
|
base.OnAuthorization(filterContext);
|
|
string ss = base.Roles;
|
|
var isAuthorized = filterContext.HttpContext.Items[IS_AUTHORIZED] != null
|
|
? Convert.ToBoolean(filterContext.HttpContext.Items[IS_AUTHORIZED])
|
|
: false;
|
|
|
|
if (!isAuthorized && filterContext.RequestContext.HttpContext.User.Identity.IsAuthenticated)
|
|
{
|
|
filterContext.RequestContext.HttpContext.Response.Redirect(RedirectUrl);
|
|
}
|
|
|
|
//记录方法埋点
|
|
menuLogHelper.AddMenuLog();
|
|
|
|
}
|
|
}
|
|
|
|
public class AuthorizeSession : FilterAttribute, IActionFilter
|
|
{
|
|
public bool HasRight { get; set; }
|
|
|
|
public void OnActionExecuting(ActionExecutingContext filterContext)
|
|
{
|
|
//throw new NotImplementedException();
|
|
var userinfo = JsonHelper.JsonDivertToObj<UserInfo>(((FormsIdentity)filterContext.HttpContext.User.Identity).Ticket.UserData);
|
|
var userRoleCodes = DataCacheHelper.GetCache().Get_RoleCodes(userinfo.userRoleId);
|
|
//LogHelper.Info(((FormsIdentity) filterContext.HttpContext.User.Identity).Ticket.UserData);
|
|
//LogHelper.Info(userRoleCodes);
|
|
bool right = userRoleCodes.Contains("[GLY]") || userRoleCodes.Contains("[CW]") || userRoleCodes.Contains("[ZJ]") || userRoleCodes.Contains("[ZJZL]");
|
|
if (right)
|
|
{
|
|
var session = filterContext.HttpContext.Session["AuthorizeSession"];
|
|
if (session == null || string.IsNullOrEmpty(session.ToString()))
|
|
{
|
|
filterContext.HttpContext.Response.Redirect("/Base/Error/AuthorizeSession?returnUrl=" + filterContext.HttpContext.Request.RawUrl);
|
|
}
|
|
}
|
|
//if (!HasRight)
|
|
//{
|
|
// if (userRoleCodes.Contains("GLY") || userRoleCodes.Contains("CW") || userRoleCodes.Contains("ZJ") || userRoleCodes.Contains("ZJZL"))
|
|
// {
|
|
// HasRight = true;
|
|
// }
|
|
//}
|
|
//LogHelper.Info("HasRight:" + HasRight);
|
|
//if (HasRight)
|
|
//{
|
|
// var session = filterContext.HttpContext.Session["AuthorizeSession"];
|
|
// if (session == null || string.IsNullOrEmpty(session.ToString()))
|
|
// {
|
|
// filterContext.HttpContext.Response.Redirect("/Base/Error/AuthorizeSession?returnUrl=" + filterContext.HttpContext.Request.RawUrl);
|
|
// }
|
|
//}
|
|
}
|
|
|
|
public void OnActionExecuted(ActionExecutedContext filterContext)
|
|
{
|
|
//throw new NotImplementedException();
|
|
}
|
|
}
|
|
} |