public override void OnAuthorization(AuthorizationContext filterContext) { var controller = filterContext.Controller as BaseController; if (controller != null) { var userViewModel = new UserViewModel { IsLoggedIn = false }; if (filterContext.HttpContext.Request.Cookies[UserCookieName] != null) { string cookie = filterContext.HttpContext.Request.Cookies[UserCookieName].Value; byte[] cipherText = Convert.FromBase64String(cookie); string plainText = cipherText.Decrypt(); int id; if (int.TryParse(plainText, out id)) { User user = UserService.GetUserWithTheirBlogs(id); if (user != null) { userViewModel.Id = id; userViewModel.Email = user.Email; userViewModel.Name = user.Name; userViewModel.IsLoggedIn = true; userViewModel.AddNicknamesToUser(user); } } } filterContext.HttpContext.User = Thread.CurrentPrincipal = userViewModel; } }
private void UpdateCookiesAndContext(User user) { byte[] cipherText = user.Id.ToString().Encrypt(); string base64CipherText = Convert.ToBase64String(cipherText); Response.Cookies.Add(new HttpCookie(GetCookieUserFilterAttribute.UserCookieName, base64CipherText)); var userViewModel = new UserViewModel { Email = user.Email, Name = user.Name, IsLoggedIn = true }; userViewModel.AddNicknamesToUser(user); HttpContext.User = userViewModel; }
private UserViewModel UpdateCookies(User user) { var userViewModel = new UserViewModel { Email = user.Email, Name = user.Name, IsLoggedIn = true, Id = user.Id }; if (_filterContext.HttpContext.Request.Cookies[GetCookieUserFilterAttribute.UserCookieName] == null) { byte[] cipherText = user.Id.ToString().Encrypt(); string base64CipherText = Convert.ToBase64String(cipherText); _filterContext.HttpContext.Response.Cookies.Add(new HttpCookie(GetCookieUserFilterAttribute.UserCookieName, base64CipherText)); } userViewModel.AddNicknamesToUser(user); return(userViewModel); }