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会话属性的主要内容,如果未能解决你的问题,请参考以下文章

csharp asp.net mvc模块属性

csharp MVC操作筛选器允许上载安全文件属性

csharp ASP.NET MVC的文件最大大小验证属性

csharp 字符串属性允许使用下拉框和MVC模型验证

csharp ASP.NET MVC 3的信用卡验证器属性

csharp MVC属性,用于将非HTTPS请求重定向到HTTPS URI。