123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- 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<string> userData = new List<string>();
- UserView userpageResult;
- using (var scope = AutofacHelper.Container.BeginLifetimeScope())
- {
- IUserServices userServices = scope.Resolve<IUserServices>();
- 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);
- }
- }
- }
|