Exemplo n.º 1
0
 /// <summary>
 /// 获取当前用户
 /// </summary>
 /// <param name="context">上下文对象</param>
 /// <returns></returns>
 public static UserInfo GetCurretnUser(HttpContext context)
 {
     lock (locker)
     {
         try
         {
             if (context == null)
             {
                 return(null);
             }
             FormsIdentity identity = context.User.Identity as FormsIdentity;
             if (identity != null)
             {
                 //取用户基本信息
                 UserInfo       userInfo   = JsonHelper.Deserialize <UserInfo>(identity.Ticket.UserData);
                 UserExtendBase userExtend = GetUserExtendCache(userInfo.UserName);
                 if (userInfo.UserName != "admin" && userExtend == null)
                 {
                     //重新加载用户扩展信息
                     userExtend = UserExtendEventHandler.GetUserExtendInfo(userInfo);
                 }
                 userInfo.ExtendUserObject = userExtend;
                 return(userInfo);
             }
         }
         catch { }
         return(null);
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// 获取当前用户
 /// </summary>
 /// <param name="context">上下文对象</param>
 /// <returns></returns>
 public static UserInfo GetCurretnUser(HttpContext context)
 {
     lock (locker)
     {
         try
         {
             if (context == null)
             {
                 return(null);
             }
             AuthenticateResult result = context.AuthenticateAsync().Result;
             if (result == null || result.Principal == null || result.Principal.Claims == null || result.Principal.Claims.Count() == 0)
             {
                 return(null);
             }
             //取用户基本信息
             Claim claim = result.Principal.Claims.Where(x => x.Type == ClaimTypes.UserData).FirstOrDefault();
             if (claim != null && !string.IsNullOrEmpty(claim.Value))
             {
                 UserInfo userInfo = JsonHelper.Deserialize <UserInfo>(claim.Value);
                 if (userInfo != null)
                 {
                     UserExtendBase userExtend = GetUserExtendCache(userInfo.UserName);
                     if (userInfo.UserName != "admin" && userExtend == null)
                     {
                         //重新加载用户扩展信息
                         userExtend = UserExtendEventHandler.GetUserExtendInfo(userInfo);
                     }
                     userInfo.ExtendUserObject = userExtend;
                     try
                     {
                         if (string.IsNullOrEmpty(userInfo.ClientIP))
                         {
                             userInfo.ClientIP = Globals.GetClientIp(context.Request);
                         }
                     }
                     catch { }
                     return(userInfo);
                 }
             }
         }
         catch { }
         return(null);
     }
 }