MVC用户权限验证

MVC用户权限验证 新增UserAuthorizeAttribute类

using Dw.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LoanH5APP_2020.Models
{
    /// <summary>
    /// 用户权限验证
    /// </summary>
    public class UserAuthorizeAttribute : AuthorizeAttribute
    {
        /// <summary>
        /// 判断用户是否登录成功
        /// 登录成功返回true,否者返回false
        /// 返回false将读取web.config中的loginUrl跳转到登录页面
        /// </summary>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var isAuthorized = false;
            if (httpContext != null && httpContext.Session != null)
            {
                if (httpContext.Session["User"] != null)
                {
                    isAuthorized = true;
                }
            }
            return isAuthorized;
        }
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
        }
    }
}

webconfig增加配置项 <system.web>下

<authentication mode="Forms">
      <forms loginUrl="~/Home/PreIndex" timeout="2880" />
    </authentication>

 

posted @ 2021-03-24 09:32  代码沉思者  阅读(79)  评论(0编辑  收藏  举报