using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; using Mini.Common; using Mini.Model.ViewModel; using Mini.Services; using Mini.Web.WebHelper; using Mini.Web.WebHelper.Models; using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Security.Principal; namespace Mini.Web.Areas.Admin.Controllers { public abstract class BaseController : Controller { private UserInfo _userinfo; private string[] _rights; private bool needToRedirect; private bool rightRedirct = false; private readonly ICacheService _cacheservice = new CacheService(); protected IHttpContextAccessor _accessor; protected BaseController() { HttpContext content = MvcContext.GetContext(); _rights = UserRightsHelper.getUserRights(content); if (_rights.Contains("-1")) { rightRedirct = true; } var authenticate = content.AuthenticateAsync("MyCookies"); if (authenticate.Result.Succeeded) { IIdentity id = content.User.Identity; string userdata = content.User.FindFirstValue(ClaimTypes.UserData); if (userdata != null) { try { _userinfo = Newtonsoft.Json.JsonConvert.DeserializeObject(userdata); } catch (Exception e) { _userinfo = new UserInfo(); LogHelper.Error(e.ToString()); } } needToRedirect = false; } else { _userinfo = new UserInfo(); needToRedirect = true; // Redirect("Base/Account/LogOn"); } } public override void OnActionExecuting(ActionExecutingContext filterContext) { if (this.needToRedirect || rightRedirct) { var account = filterContext.HttpContext.Request.Query["weixincrmaccount"]; if (!string.IsNullOrEmpty(account) && needToRedirect == true) { //HttpContext.SignOutAsync("MyCookies"); //string RedirectLocation = string.Format("~/Admin/Account/OutLogon?eid={0}&ReturnUrl={1}", account, filterContext.HttpContext.Request.Path + filterContext.HttpContext.Request.QueryString); //filterContext.Result = new RedirectResult(RedirectLocation); return; } else if (!string.IsNullOrEmpty(account) && needToRedirect == false) { //HttpContext.SignOutAsync("MyCookies"); //string RedirectLocation = string.Format("~/Admin/Account/UnAuthorized?ReturnUrl={0}", filterContext.HttpContext.Request.Path); //filterContext.Result = new RedirectResult(RedirectLocation); return; } else { //FormsAuthentication.SignOut(); HttpContext.SignOutAsync("MyCookies"); string RedirectLocation = string.Format("~/Admin/Account/LogOn?ReturnUrl={0}", filterContext.HttpContext.Request.Path); filterContext.Result = new RedirectResult(RedirectLocation); return; } } } //public override void OnException(ExceptionContext filterContext) //{ // string controllerName = filterContext.RouteData.Values["controller"].ToString(); // string actionName = filterContext.RouteData.Values["action"].ToString(); // string msg = string.Concat(controllerName, "-", actionName, ";"); // var dbEx = filterContext.Exception as DbEntityValidationException; // if (dbEx != null) // { // foreach (var validationErrors in dbEx.EntityValidationErrors) // { // msg += validationErrors.Entry.Entity.ToString(); // foreach (var validationError in validationErrors.ValidationErrors) // { // msg += string.Format("。Property:{0} Error:{1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine; // } // } // } // else // { // msg += filterContext.Exception.ToString() + ";" + filterContext.Exception.StackTrace; // } // LogHelper.Error(msg); // if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest()) // { // filterContext.HttpContext.Response.StatusCode = 200; // filterContext.ExceptionHandled = true; // filterContext.Result = new JsonResult // { // ContentType = "text/html", // Data = new // { // type = 0, // message = "系统错误:" + filterContext.Exception.Message, // errorMessag = "系统错误:" + filterContext.Exception.Message // }, // JsonRequestBehavior = JsonRequestBehavior.AllowGet // }; // } // else // { // //filterContext.ExceptionHandled = true; // //string msg = string.Concat(controllerName, "_", actionName, ";", filterContext.Exception.Message); // //filterContext.Result = new RedirectResult(Url.Action("ErrorView", "Error", new { message = msg })); // // filterContext.Result = new PartialViewResult("/Bas/Error/ErrorView", new { message = msg}); // base.OnException(filterContext); // } // // JsonHandler.ExceptionMessage(filterContext.Exception.Message); //} //protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior) //{ // return new ConfigurableJsonResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior }; //} //public bool IsLogin //{ // get // { // if (System.Web.HttpContext.Current.Request.IsAuthenticated) // return true; // else // return false; // } //} public string UserName { get { return _userinfo.userName; } } /// /// 员工工号 /// public decimal Eid { get { return _userinfo.userEid; } } /// /// 员工Id /// public int UserId { get { return _userinfo.userId; } } /// /// 员工组别id /// public decimal userGroupId { get { return _userinfo.userGroupId; } } /// /// 员工角色id /// public int[] userRoleId { get { return _userinfo.userRoleId; } } /// /// 员工角色id /// public string[] userRoleNames { get { return _userinfo.userRoleName; } } public decimal LoginLogId { get { return _userinfo.logInLogID; } } /// /// 员工权限id /// public string[] userRightId { get { return _rights; } } public string[] userRoleCodes { get { return _userinfo.userRoleCode; } //get { return null; } } public List QwDeptControlList { get { return _userinfo.qwdeptcontrolList; } } } }