csharp MVC 4会话属性
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp MVC 4会话属性相关的知识,希望对你有一定的参考价值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using WebPortal.ViewModels.SystemUser;
using WebPortal.Helpers;
using WebPortal.Models;
namespace WebPortal.Helpers
{
public class SessionAttribute : ActionFilterAttribute
{
//
// GET: /SessionAttribute/
string actionName = System.Web.HttpContext.Current.Request.RequestContext.RouteData.GetRequiredString("action").ToLower();
string controllerName = System.Web.HttpContext.Current.Request.RequestContext.RouteData.GetRequiredString("controller").ToLower();
//string actionId = System.Web.HttpContext.Current.Request.RequestContext.RouteData.GetRequiredString("id");
SystemUser sysUser = new SystemUser();
List<UserPermission> uPer = new List<UserPermission>();
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Session["SystemUser"] == null)
{
var values = new
{
controller = "User",
action = "Index",
returnUrl = filterContext.HttpContext.Request.Url
};
var result = new RedirectToRouteResult("Default", new RouteValueDictionary(values));
filterContext.Result = result;
}
else
{
sysUser = HttpContext.Current.Session["SystemUser"] as SystemUser;
uPer = HttpContext.Current.Session["lstUserPermissions"] as List<UserPermission>;
if (sysUser.ruleType == true && actionName == "Index" && controllerName == "User")
{
var values = new
{
controller = "Dashboard",
action = "Index",
// returnUrl = filterContext.HttpContext.Request.Url
};
var result = new RedirectToRouteResult("Default", new RouteValueDictionary(values));
filterContext.Result = result;
}
else if (actionName == "Index" && controllerName == "User")
{
var values = new
{
controller = "profile",
action = "details",
id = sysUser.profileId
// returnUrl = filterContext.HttpContext.Request.Url
};
var result = new RedirectToRouteResult("Default", new RouteValueDictionary(values));
filterContext.Result = result;
}
else
{
var vPer = uPer.Find(f => f.C_controllerName.ToLower() == controllerName && f.C_actionName.ToLower() == actionName);
if (sysUser.ruleType != vPer.C_isAdmin)
{
var values = new
{
controller = "profile",
action = "details",
id = sysUser.profileId
// returnUrl = filterContext.HttpContext.Request.Url
};
var result = new RedirectToRouteResult("Default", new RouteValueDictionary(values));
filterContext.Result = result;
}
}
}
}
}
}
以上是关于csharp MVC 4会话属性的主要内容,如果未能解决你的问题,请参考以下文章