using System;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System.Linq;
using DFS.Core.Mvc.Jwt.Model;
namespace DFS.Core.Mvc.Jwt
{
///
/// 扩展httpcontext 当前授权的用户
///
public static class HttpContextExcention
{
///
/// 获取当前授权用户信息
///
///
///
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(str);
}
return null;
}
///
/// 获取当前授权的客户端信息
///
///
///
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(str);
}
return null;
}
///
/// 扩展客户端加密比较
///
///
/// 请求的参数body
/// 秘钥
///
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);
}
}
}