全局判定用户登录

新建新的类BaseController

重写Controller中的OnActionExecuting方法

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using StudentCall.Models;


namespace StudentCall.Controllers
{
    public class BaseController : Controller
    {

        /// <summary>
        /// 对继承者进行全局OnActionExecuting定义
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string Username = HttpContext.Session.GetString("username");//启用Session后才可以使用Session
            HttpContext.Session.SetString("username", "admin");
            if (string.IsNullOrEmpty(Username))
            {
                filterContext.Result = Redirect("/Login");
            }
         
        }
    }
}

 

需要验证的界面只需要对BaseController进行继承就可以

OnActionExecuting方法的重写相当于WebFrom中的Load事件

posted @ 2018-12-04 22:21  Tenk的园子  阅读(213)  评论(0编辑  收藏  举报