Ejemplo n.º 1
0
        /// <summary>
        /// Remembers a user's information.
        /// </summary>
        /// <param name="user">User to remember.</param>
        /// <param name="context"><c>HttpContextBase</c> to store the user information.</param>
        public void RememberUser(User user, HttpContext context)
        {
            HttpCookie cookie = new HttpCookie("id", user.Id) { Expires = DateTime.MaxValue, HttpOnly = true };
            context.Response.Cookies.Set(cookie);

            if (!String.IsNullOrEmpty(user.Name))
            {
                cookie = new HttpCookie("n", user.Name) { Expires = DateTime.MaxValue, HttpOnly = true };
                context.Response.Cookies.Set(cookie);
            }

            if (!String.IsNullOrEmpty(user.Email))
            {
                cookie = new HttpCookie("e", user.Email) { Expires = DateTime.MaxValue, HttpOnly = true };
                context.Response.Cookies.Set(cookie);
            }

            if (!String.IsNullOrEmpty(user.Link))
            {
                cookie = new HttpCookie("l", user.Link) { Expires = DateTime.MaxValue, HttpOnly = true };
                context.Response.Cookies.Set(cookie);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a principaled user from the HTTP context.
        /// </summary>
        /// <param name="principal"><c>IPrincipal</c> for user.</param>
        /// <param name="context"><c>HttpContextBase</c> to populate the user information.</param>
        /// <returns>User object.</returns>
        private User CreateFromPrincipal(IPrincipal principal, HttpContext context)
        {
            User user = new User(principal);
            HttpCookie cookie;

            // Ensure the user has a unique identifier.
            if (principal.IsInRole("Authenticated"))
            {
                user.Id = this.AdminId;
            }
            else
            {
                Guid guid;

                cookie = context.Request.Cookies["id"];
                if (null != cookie)
                {
                    try
                    {
                        guid = new Guid(cookie.Value);
                    }
                    catch (Exception)
                    {
                        guid = Guid.NewGuid();
                    }

                    if (null != (cookie = context.Request.Cookies["n"]))
                    {
                        user.Name = cookie.Value;
                    }

                    if (null != (cookie = context.Request.Cookies["e"]))
                    {
                        user.Email = cookie.Value;
                    }

                    if (null != (cookie = context.Request.Cookies["l"]))
                    {
                        user.Link = cookie.Value;
                    }
                }
                else
                {
                    guid = Guid.NewGuid();
                }

                user.Id = guid.ToString("N");
            }

            cookie = new HttpCookie("id", user.Id) { Expires = DateTime.MaxValue, HttpOnly = true };
            context.Response.Cookies.Set(cookie);

            // Set whether this user is the admin user.
            if (user.Id == this.AdminId)
            {
                user.Administrator = true;
            }

            // Count the number of times this user has visited the site.
            cookie = context.Request.Cookies["vc"];
            if (cookie != null)
            {
                int visitCount = 0;
                if (Int32.TryParse(cookie.Value, out visitCount))
                {
                    user.Visits = visitCount + 1;
                }
            }

            cookie = new HttpCookie("vc", user.Visits.ToString()) { Expires = DateTime.MaxValue, HttpOnly = true };
            context.Response.Cookies.Set(cookie);

            // Capture other useful information about the user.
            user.IP = context.Request.UserHostAddress;
            user.ReferrerUrl = context.Request.UrlReferrer;
            user.Url = context.Request.Url;
            user.UserAgent = context.Request.UserAgent;

            return user;
        }