You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DSWMS/开发版dev/Vue.NetCore/Vue.Net/VOL.Core/Filters/FixedTokenAttribute.cs

54 lines
1.9 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Net;
using System.Security.Claims;
using VOL.Core.Configuration;
using VOL.Core.Extensions;
using VOL.Core.ManageUser;
using VOL.Core.Utilities;
namespace VOL.Core.Filters
{
public interface IFixedTokenFilter : IFilterMetadata
{
AuthorizationFilterContext OnAuthorization(AuthorizationFilterContext context);
}
public class FixedTokenAttribute : Attribute, IFixedTokenFilter, IAllowAnonymous
{
public AuthorizationFilterContext OnAuthorization(AuthorizationFilterContext context)
{
string fixedoken = "";
//如果token已失效直接获取header里的token
if (!context.HttpContext.User.Identity.IsAuthenticated)
{
fixedoken = context.HttpContext.Request.Headers[AppSetting.TokenHeaderName];
fixedoken = fixedoken?.Replace("Bearer ", "");
//判断是否传入了token
if (string.IsNullOrEmpty(fixedoken))
{
return context.Unauthorized("没有传入token");
}
//解析token
int userId = JwtHelper.GetUserId(fixedoken);
if (userId <= 0)
{
return context.Unauthorized("token不正确");
}
context.AddIdentity(userId);
}
else
{
fixedoken = ((ClaimsIdentity)context.HttpContext.User.Identity)
?.BootstrapContext?.ToString();
}
//判断当前用户的token与缓存的token是否相同
if (UserContext.Current.Token != fixedoken)
{
context.FilterResult(HttpStatusCode.Unauthorized, "token已失效");
}
return context;
}
}
}