BXXT/Filter/CheckLogin.cs

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BXXT.Models;
using System.Diagnostics;
namespace BXXT.Filter
{
    /// <summary>
    /// 这个过滤器用来判断用户是否登录
    /// </summary>
    public class CheckLogin:ActionFilterAttribute
    {
        /// <summary>
        /// Action运行之前验证用户是否登录
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.Session["UserType"]==null)
            {
                //如果用户还没登录跳转到登录页面

                filterContext.Result = new RedirectResult("/Login/Index");
            }
            else
            {
                string TargetAction = filterContext.RouteData.Values["Action"].ToString();//取出目标Action
                switch (TargetAction)
                {
                    case "Student":
                        //如果想要访问学生页面
                        if (Convert.ToInt32(filterContext.HttpContext.Session["UserType"]) != (int)LoginType.StudentLogin)
                        {
                            //如果登录类型和想要访问的页面不一致
                            filterContext.Result = new RedirectResult("/Login/Index");//直接跳转回登录页面重新选择
                        }
                        break;
                    case "Guy":
                        //如果想要访问维修工页面
                        if (Convert.ToInt32(filterContext.HttpContext.Session["UserType"]) != (int)LoginType.GuyLogin)
                        {
                            //如果登录类型和想要访问的页面不一致
                            filterContext.Result = new RedirectResult("/Login/Index");//直接跳转回登录页面重新选择
                        }
                        break;
                    case "Admin":
                        //如果想要访问管理员页面
                        if (Convert.ToInt32(filterContext.HttpContext.Session["UserType"]) != (int)LoginType.AdminLogin)
                        {
                            //如果登录类型和想要访问的页面不一致
                            filterContext.Result = new RedirectResult("/Login/Index");//直接跳转回登录页面重新选择
                        }
                        break;
                        
                    default:
                    //如果要访问其他页面
                    filterContext.Result = new RedirectResult("/Login/Index");//直接跳转回登录页面重新选择
                        break;
后面还有 7 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。