Merge branch 'master' into v3.1
大石头 authored at 2024-07-13 19:03:18
724.00 B
Stardust
using System;
using Microsoft.AspNetCore.Mvc.Filters;
using NewLife;
using NewLife.Remoting;
using Stardust.Server.Controllers;

namespace Stardust.Server.Common
{
    /// <summary>令牌校验</summary>
    public class TokenFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (context.Controller is BaseController bc)
            {
                var session = bc.Session;
                if (bc.Token.IsNullOrEmpty()) throw new ApiException(403, "未授权");
                if (session == null) throw new ApiException(402, "令牌无效");
            }

            base.OnActionExecuting(context);
        }
    }
}