1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- using System;
- using Microsoft.AspNetCore.Http;
- using Newtonsoft.Json;
- using System.Linq;
- using DFS.Core.Mvc.Jwt.Model;
- namespace DFS.Core.Mvc.Jwt
- {
- /// <summary>
- /// 扩展httpcontext 当前授权的用户
- /// </summary>
- public static class HttpContextExcention
- {
-
- /// <summary>
- /// 获取当前授权用户信息
- /// </summary>
- /// <param name="httpContext"></param>
- /// <returns></returns>
- public static TokenModel GetCurrentUser(this IHttpContextAccessor httpContext, IAuhUser user)
- {
- var str = httpContext.HttpContext.User.Claims.FirstOrDefault(p => p.Type == JwtConfig.TokenUser)?.Value;
- if (!string.IsNullOrWhiteSpace(str))
- {
- return JsonConvert.DeserializeObject<TokenModel>(str);
- }
- return null;
- }
- /// <summary>
- /// 获取当前授权的客户端信息
- /// </summary>
- /// <param name="httpContext"></param>
- /// <returns></returns>
- public static OpenClient GetCurrentClient(this IHttpContextAccessor httpContext)
- {
- var str = httpContext.HttpContext.User.Claims.FirstOrDefault(p => p.Type == "OpenApp")?.Value;
- if (!string.IsNullOrWhiteSpace(str))
- {
- return JsonConvert.DeserializeObject<OpenClient>(str);
- }
- return null;
- }
- /// <summary>
- /// 扩展客户端加密比较
- /// </summary>
- /// <param name="httpContextAccessor"></param>
- /// <param name="body">请求的参数body</param>
- /// <param name="key">秘钥</param>
- /// <returns></returns>
- public static bool AuthSign(this IHttpContextAccessor httpContextAccessor, string body, string key)
- {
- var client = httpContextAccessor.GetCurrentClient();
- if (client == null)
- {
- return false;
- }
- if ((client?.Sign ?? "").IsNullOrWhiteSpace())
- {
- return false;
- }
- return string.Equals(SignatureHelper.GetSignStr(client.ToMapDictionary(), body, key),
- client.Sign);
- }
- }
- }
|