using System; using System.Collections.Generic; using System.Linq; using System.Web; using Autofac; using Bowin.Common; using EMISOnline.Utility; using EMISOnline.CommonLogic.SystemServices; using EMISOnline.ViewModel.Account; using System.Web.Mvc; using EMISOnline.ViewModel.SystemView; namespace EMISOnline.Web.Controls { public static class BaseExtensions { public static void GetContextUser() { FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper(); var cookieName = EMISOnline.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME; fahelper.AuthenticateRequest(HttpContext.Current, cookieName, Const.LOCAL_AUTH_EXCEPTURL); } public static void LoginSureccessful(this ControllerBase controller, LogOnModel model, string userName) { FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper(); var cookieName = EMISOnline.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME; List userData = new List(); UserView userpageResult; using (var scope = AutofacHelper.Container.BeginLifetimeScope()) { IUserServices userServices = scope.Resolve(); userpageResult = userServices.GetUserByLoginID(userName, true); if (userpageResult == null) { throw new Exception("获取用户失败。"); } } if (model.RememberMe) { controller.RemeberUserCookies(model); } else { controller.RemoveUserCookies(model); } userData.Add(userpageResult.LoginID); userData.Add(userpageResult.UserID.ToString()); userData.Add(userpageResult.Name); userData.Add(userpageResult.StaffID.HasValue ? userpageResult.StaffID.Value.ToString() : "");//教职工ID userData.Add(userpageResult.StudentID.HasValue ? userpageResult.StudentID.Value.ToString() : "");//学生ID userData.Add(userpageResult.DepartmentID.HasValue ? userpageResult.DepartmentID.Value.ToString() : "");//部门(教研室)ID userData.Add(userpageResult.DepartmentName ?? "");//部门(教研室)树结构ID userData.Add(userpageResult.HierarchyID ?? "");//部门(教研室)名称 userData.Add(userpageResult.CollegeID.HasValue ? userpageResult.CollegeID.Value.ToString() : ""); userData.Add(userpageResult.CampusID.HasValue ? userpageResult.CampusID.Value.ToString() : ""); userData.Add(userpageResult.UniversityID.HasValue ? userpageResult.UniversityID.Value.ToString() : ""); userData.Add(userpageResult.RoleID.HasValue ? userpageResult.RoleID.Value.ToString() : ""); fahelper.loginFormsAuthentication(controller.ControllerContext.HttpContext, cookieName, userData); } public static void RemoveUserCookies(this ControllerBase controller, LogOnModel model) { HttpCookie cookie = new HttpCookie("username"); cookie.Value = model.UserName; cookie.Expires = DateTime.Now.AddDays(-1); controller.ControllerContext.HttpContext.Response.AppendCookie(cookie); HttpCookie cookie2 = new HttpCookie("password"); cookie2.Value = model.Password; cookie2.Expires = DateTime.Now.AddDays(-1); controller.ControllerContext.HttpContext.Response.AppendCookie(cookie2); HttpCookie cookie3 = new HttpCookie("rememberme"); cookie3.Value = model.RememberMe.ToString(); cookie3.Expires = DateTime.Now.AddDays(-1); controller.ControllerContext.HttpContext.Response.AppendCookie(cookie3); } public static void RemeberUserCookies(this ControllerBase controller, LogOnModel model) { HttpCookie cookie = new HttpCookie("username"); cookie.Value = model.UserName; cookie.Expires = DateTime.Now.AddDays(1); controller.ControllerContext.HttpContext.Response.AppendCookie(cookie); HttpCookie cookie2 = new HttpCookie("password"); cookie2.Value = model.Password; cookie2.Expires = DateTime.Now.AddDays(1); controller.ControllerContext.HttpContext.Response.AppendCookie(cookie2); HttpCookie cookie3 = new HttpCookie("rememberme"); cookie3.Value = model.RememberMe.ToString(); cookie3.Expires = DateTime.Now.AddDays(1); controller.ControllerContext.HttpContext.Response.AppendCookie(cookie3); } } }