public override void OnActionExecuting(ActionExecutingContext filterContext) { if (!filterContext.HttpContext.User.Identity.IsAuthenticated) { filterContext.Controller.ViewBag.UserAuthInfo = null; base.OnActionExecuting(filterContext); return; } var frmId = (FormsIdentity)filterContext.HttpContext.User.Identity; var usData = frmId.Ticket.UserData; if (string.IsNullOrEmpty(usData)) { filterContext.Controller.ViewBag.UserAuthInfo = null; base.OnActionExecuting(filterContext); return; } var userDataSplit = usData.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); if (!userDataSplit.Any() || userDataSplit.Length != 3) { filterContext.Controller.ViewBag.UserAuthInfo = null; base.OnActionExecuting(filterContext); return; } if (!DataCheck.IsNumeric(userDataSplit[0].Trim())) { filterContext.Controller.ViewBag.UserAuthInfo = null; base.OnActionExecuting(filterContext); return; } var roles = userDataSplit[2].Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); var identity = new FormsIdentity(frmId.Ticket); var principal = new StcPrincipal(identity, roles); var userData = new UserData { UserId = long.Parse(userDataSplit[0].Trim()), Username = frmId.Name, Email = userDataSplit[1].Trim(), Roles = roles, }; if (!MvcApplication.SetUserData(userData)) { filterContext.Controller.ViewBag.UserAuthInfo = null; base.OnActionExecuting(filterContext); return; } filterContext.Controller.ViewBag.UserAuthInfo = userData; filterContext.HttpContext.User = principal; base.OnActionExecuting(filterContext); }
void Application_AuthenticateRequest() { var myCookie = FormsAuthentication.FormsCookieName; var myAuthCookie = Context.Request.Cookies[myCookie]; if (null == myAuthCookie) { return; } FormsAuthenticationTicket myAuthTicket; try { myAuthTicket = FormsAuthentication.Decrypt(myAuthCookie.Value); } catch (Exception ex) { BugManager.LogApplicationBug(ex.StackTrace, ex.Source, ex.Message); return; } if (null == myAuthTicket) { return; } var userDataSplit = myAuthTicket.UserData.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); var size = userDataSplit.Length; //if (!userDataSplit.Any() || userDataSplit.Length != 3) //if (!userDataSplit.Any()) //{ // if ((size != 3 || size != 4)) // { // return; // } //} if (!userDataSplit.Any() || userDataSplit.Length != 3) { return; } if (!DataCheck.IsNumeric(userDataSplit[0].Trim())) { return; } //switch (size) //{ // case 3: // var roles = userDataSplit[2].Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); // var id = new FormsIdentity(myAuthTicket); // //IPrincipal principal = new StcPrincipal(id, roles); // //Context.User = principal; // break; // case 4: // var clientRoles = userDataSplit[3].Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); // var clientId = new FormsIdentity(myAuthTicket); // //IPrincipal clientPrincipal = new StcPrincipal(clientId, clientRoles); // //Context.User = clientPrincipal; // break; //} var roles = userDataSplit[2].Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); //var role = userDataSplit[2]; var id = new FormsIdentity(myAuthTicket); IPrincipal principal = new StcPrincipal(id, roles); Context.User = principal; }