软测单独项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

61 lines
2.0 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Script.Serialization;
namespace FangYar.Common
{
/// <summary>
/// 用户帮助类:获取Cookie里的用户对象
/// </summary>
public class UserHelper
{
/// <summary>
/// 获取保存在cookie里的用户对象
/// </summary>
public static FangYar.Model.LoginUserModel GetUser(HttpContext context)
{
try
{
//if (context.Request.IsAuthenticated)
//{
// FormsIdentity id = (FormsIdentity)context.User.Identity;
// FormsAuthenticationTicket tickets = id.Ticket;
// //反序列化获取票证里序列化的用户对象
// FangYar.Model.LoginUserModel userFromCookie = new JavaScriptSerializer().Deserialize<FangYar.Model.LoginUserModel>(tickets.UserData);
// return userFromCookie;
//}
//else
// return null;
var cook = context.Request.Cookies;
var cookInfo = cook["kn_root_cookie"];
if (cookInfo != null)
{
var str = cookInfo.Value;
//判断是否存在登录缓存信息
if (!string.IsNullOrWhiteSpace(str))
{
var usersUid = str.Substring(str.IndexOf("\"usersUid\":\"") + 12);
usersUid = usersUid.Substring(0, usersUid.IndexOf("\","));
FangYar.Model.LoginUserModel userFromCookie = new JavaScriptSerializer().Deserialize<FangYar.Model.LoginUserModel>(str);
return userFromCookie;
}
}
}
catch (Exception ex)
{
return null;
}
return null;
}
}
}