using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Extensions; using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Security.Claims; using System.Text; using System.Threading.Tasks; using WIDESEA_Core.Helper; namespace WIDESEA_Core.Extensions { public static class SwaggerContextExtension { public const string SwaggerCodeKey = "swagger-code"; public const string SwaggerJwt = "swagger-jwt"; /// /// 检查当前Swagger请求是否验证成功 /// /// 如果Session中Swagger验证码为"success"则返回true,否则返回false public static bool IsSuccessSwagger() { return App.HttpContext?.GetSession()?.GetString(SwaggerCodeKey) == "success"; } /// /// 判断当前Swagger请求是否已通过验证 /// /// Http上下文对象 /// 如果Swagger验证成功返回true,否则返回false public static bool IsSuccessSwagger(this HttpContext context) { return context.GetSession()?.GetString(SwaggerCodeKey) == "success"; } /// /// 设置Swagger验证成功状态 /// /// /// 将Swagger验证状态码"success"存储到当前会话中 /// public static void SuccessSwagger() { App.HttpContext?.GetSession()?.SetString(SwaggerCodeKey, "success"); } /// /// 设置Swagger操作成功的状态码 /// /// Http上下文对象 public static void SuccessSwagger(this HttpContext context) { context.GetSession()?.SetString(SwaggerCodeKey, "success"); } /// /// 为Swagger JWT认证设置成功上下文 /// /// Http上下文对象 /// JWT令牌 /// /// 将JWT令牌解析为ClaimsIdentity并添加到用户身份中,同时将令牌存储在会话中 /// public static void SuccessSwaggerJwt(this HttpContext context, string token) { var claims = new ClaimsIdentity(GetClaimsIdentity(token)); context.User.AddIdentity(claims); context.GetSession().SetString(SwaggerJwt, token); } /// /// 从JWT令牌中获取声明(Claims)集合 /// /// JWT令牌字符串 /// 包含声明信息的集合,若令牌无效则返回空集合 private static IEnumerable GetClaimsIdentity(string token) { var jwtHandler = new JwtSecurityTokenHandler(); // token校验 if (token.IsNotEmptyOrNull() && jwtHandler.CanReadToken(token)) { var jwtToken = jwtHandler.ReadJwtToken(token); return jwtToken.Claims; } return new List(); } /// /// 从当前HTTP上下文中获取成功的Swagger JWT令牌 /// /// HTTP上下文 /// Swagger JWT令牌字符串 public static string GetSuccessSwaggerJwt(this HttpContext context) { return context.GetSession().GetString(SwaggerJwt); } /// /// 重定向到Swagger登录页面 /// /// Http上下文对象 /// /// 将当前请求重定向到/swg-login.html页面,并附带当前URL作为返回地址参数 /// public static void RedirectSwaggerLogin(this HttpContext context) { var returnUrl = context.Request.GetDisplayUrl(); //获取当前url地址 context.Response.Redirect("/swg-login.html?returnUrl=" + returnUrl); } } }