asp.net core登录/授权过滤器

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Policy;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using WebNetCore5_Img_Storage.Model;

namespace WebNetCore5_Img_Storage.Handler
{
    //IAuthorizationService
    //DefaultAuthorizationService
    //Microsoft.AspNetCore.Mvc.Filters.IAsyncAuthorizationFilter
    //Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter
    // AuthorizeAttribute
    // AuthorizationHandler
    // IAuthorizationHandler
    //AuthorizationHandler
    //IAuthorizationMiddlewareResultHandler
    //AuthorizeFilter
    // AuthorizationHandler
    //Microsoft.AspNetCore.Mvc.Filters.ActionFilterAttribute
    //   DefaultAuthorizationHandlerProvider

    /// 
    /// 检查登录,授权验证
    /// 
    public sealed class LoginFilter : Attribute, IAsyncAuthorizationFilter
    {
        public LoginFilter()
        {
        }

        public LoginFilter(string funId)
        {
            FunId = funId;
        }

        /// 
        /// 方法标识id
        /// 
        private string FunId { get; set; }

        public Task OnAuthorizationAsync(AuthorizationFilterContext context)
        {
            bool isAjax = IsAjax(context.HttpContext.Request);
            string sessionJson = context.HttpContext.Session.GetString("user");
            if (string.IsNullOrWhiteSpace(sessionJson))
            {
                if (isAjax)
                {
                    //无权访问
                    //context.Result = new UnauthorizedResult();
                    context.Result = new JsonResult(new { Code = 500, Msg = "登录失效,请重新登录" })
                    {
                        StatusCode = StatusCodes.Status401Unauthorized
                    };
                    return Task.CompletedTask;
                }
                //没有登录,去登录
                context.Result = new RedirectResult("/user/LoginView");
                return Task.CompletedTask;
            }
            if (string.IsNullOrWhiteSpace(FunId))
            {
                return Task.CompletedTask;
            }

            检查用户权限
            var userView = System.Text.Json.JsonSerializer.Deserialize<UserView>(sessionJson);
            bool have = userView.HaveMenuList.Any(x => x.Id == FunId);
            if (have)
            {
                return Task.CompletedTask;
            }         
            if (isAjax)
            {
                //无权访问
                //context.Result = new UnauthorizedResult();
                context.Result = new JsonResult(new { Code = 500, Msg = "你无权访问" })
                {
                    StatusCode = StatusCodes.Status401Unauthorized
                };
            }
            else
            {
                //无权访问
                //context.Result = new UnauthorizedResult();
                context.Result = new ContentResult()
                {
                    Content = "你无权访问",
                    StatusCode = StatusCodes.Status401Unauthorized
                };
            }
            return Task.CompletedTask;
        }

        /// 
        /// 判断是否为ajax请求
        /// 
        /// 
        /// 
        public static bool IsAjax(HttpRequest req)
        {
            //X-Requested-With: XMLHttpRequest
            bool result = false;
            var xreq = req.Headers.ContainsKey("x-requested-with");
            if (xreq)
            {
                result = req.Headers["x-requested-with"] == "XMLHttpRequest";
            }
            return result;
        }
    }
}

调用:

    //view 图册/视频固定标签管理
        [LoginFilter("50002")]
        public async Task<ActionResult> TagList(Atlas_tag atlas_Tag, int p = 1, int rows = 10)
        {
            var page = await atlasTagBLL.PageAtlas_tag(atlas_Tag, p, rows);
            var pageList = PagedListModelExtension.ToPagedList(page.List, p, rows, page.Total);
            return View(pageList);
        }

你可能感兴趣的:(asp.net,core,asp.net,core过滤器)