/// <summary> /// 获得当前的登陆用户信息 /// </summary> /// <returns></returns> public Framework.Web.Security.Authentication.LoginUserInfo GetLoginUserInfo() { string cacheKey = null; Framework.Web.Security.Authentication.LoginUserInfo result = null; string IsUservirtualUser = ConfigurationManager.AppSettings["IsVirtualUser"]; IsUservirtualUser = string.IsNullOrEmpty(IsUservirtualUser) ? "true" : IsUservirtualUser; if (HttpContext.Current == null || Convert.ToBoolean(IsUservirtualUser)) { #region // 提供一种在单元测试环境下可以模拟的机制 string mockCurrentUserLoginName = ConfigurationManager.AppSettings["VirtualUser"]; if (!string.IsNullOrEmpty(mockCurrentUserLoginName)) { cacheKey = mockCurrentUserLoginName; result = GetUser(cacheKey); } else { throw new ApplicationException("必须是在Web环境下执行此方法。"); } #endregion } else { string ssoUsername = GetMemberIdSSoCookies(); string strUserName = !string.IsNullOrEmpty(HttpContext.Current.User.Identity.Name) ? HttpContext.Current.User.Identity.Name : ssoUsername; strUserName = HttpContext.Current.Items["LoginIdentity"] != null ? HttpContext.Current.Items["LoginIdentity"].ToString() : strUserName; cacheKey = strUserName; if (string.IsNullOrEmpty(cacheKey)) { return(null); } if (!UserCache.Instance.TryGetValue(cacheKey, out result)) { #region if (GetUser != null) { result = GetUser(cacheKey); if (result == null) { //throw new Exception("该用户无权访问系统,请联系系统管理员。"); return(null); } UserCache.Instance.Add(cacheKey, result); } #endregion } } return(result); }
/// 获得当前的登陆用户信息 /// </summary> /// <param name="throwExceptionWhenNull">当true时,如果当前登录用户失效, 则抛出异常</param> /// <returns></returns> /// <exception cref="LoginUserNullException"></exception> public static Framework.Web.Security.Authentication.LoginUserInfo GetCurrentUser(bool Redeict = true) { Framework.Web.Security.Authentication.LoginUserInfo result = WebHelper.Instance.GetLoginUserInfo(); if (result != null) { LogHelper.Instance.Info(string.Format("当前登录用户姓名:{0}", result.CNName)); } //if (result == null && Redeict) //{ // HttpContext context = HttpContext.Current; // context.Response.Redirect(string.Format("{0}?returnurl={1}", System.Web.Security.FormsAuthentication.LoginUrl, context.Request.RawUrl)); //} return(result); }