//Set up user information public static void TrySetUserInfo(HttpContext context) { if (context == null) { return; } HttpCookie cookie = context.Request.Cookies[FormsAuthentication.FormsCookieName]; if (cookie == null || string.IsNullOrEmpty(cookie.Value)) { return; } try { UserViewResult userViewResult = null; FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value); if (ticket == null || string.IsNullOrEmpty(ticket.UserData)) { return; } userViewResult = (new JavaScriptSerializer()).Deserialize <UserViewResult>(ticket.UserData); if (userViewResult == null) { return; } context.User = new UserPrincipal(new UserIdentity(userViewResult)); } catch { } }
public async Task <IActionResult> View([FromRoute] UserViewQuery query) { UserViewResult value = await _mediator.Send(query); if (value == null) { return(NotFound()); } return(base.Ok(value)); }
//create cookie public static HttpCookie CreateCookie(UserViewResult userViewResult) { var userData = (new JavaScriptSerializer()).Serialize(userViewResult); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket (1, userViewResult.UserName, DateTime.Now, DateTime.Now.AddHours(12), true, userData); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket)); cookie.Expires = DateTime.Now.AddHours(12); return(cookie); }
public UserIdentity(UserViewResult user) { if (user != null) { ID = user.ID; ShortID = user.ShortID; Name = user.UserName; Role = user.Role; RealName = user.RealName; RealPost = user.RealPost; IsAuthenticated = true; } }