1 在拦截器中解析token并将用户信息加入headrs中
context.HttpContext.Request.Headers.Add("UserId", tokeninfo.UserId.ToString());
2 编写获取header中用户信息
///
/// 获取用户id
///
///
///
public static int GetTokenUserId(HttpContext data)
{
int userid = -1;
string str = "";
if (data.Request.Headers.Keys.Contains("UserId"))
{
str = data.Request.Headers["UserId"];
data.Request.Headers.Remove("UserId");
};
if (!string.IsNullOrEmpty(str))
{
int.TryParse(str, out userid);
}
return userid;
}
3 接口获取用户信息
int userid = GetTokenUserId(HttpContext);