public static string GetUserName() { User _User= new User(); string UserCode = GetUserCode(); UserInfo info = _User.GetUserInfoByUserCode(UserCode); if (info != null) return info.UserName; return ""; }
/// <summary> /// 用户登录 /// </summary> /// <param name="userName"></param> /// <param name="password"></param> /// <returns></returns> public static bool ValidUser(string userName, string password) { if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password)) { User _User = new User(); UserInfo info = _User.Login(userName, password); //password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5"); //string realPassword = Users.GetUser(userName).Password; if (info != null) { SessionHelper.Add("UserInfo", info); //if(!GenericCache<string, UserInfo>.ContainsKey(info.UserCode)) // GenericCache<string, UserInfo>.Add(info.UserCode, info); string userRoles = UserToRole(info.UserId.ToString()); //调用UserToRole方法来获取role字符串 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, info.UserCode.ToString(), DateTime.Now, DateTime.Now.AddDays(1), false, userRoles//可以将Roles按","分割成字符串,写入cookie ); string data = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, data); cookie.Path = FormsAuthentication.FormsCookiePath; cookie.Domain = FormsAuthentication.CookieDomain; cookie.Expires = ticket.Expiration; HttpContext.Current.Response.Cookies.Add(cookie); return true; } } return false; }