public ActionResult Login(string token)
        {
            if (string.IsNullOrWhiteSpace(token))
                return Error(Strings.LoginModule_BadResponse_NoToken);

            var response = new WebClient().DownloadString(string.Format("https://rpxnow.com/api/v2/auth_info?apiKey={0}&token={1}", _apikey, token));

            if (string.IsNullOrWhiteSpace(response))
                return Error(Strings.LoginModule_BadResponse_NoUser);

            var j = JsonConvert.DeserializeObject<dynamic>(response);

            if (j.stat.ToString() != "ok")
                return Error(Strings.LoginModule_BadResponse);

            var userIdentity = j.profile.identifier.ToString();
            var user = _userRepository.GetUserFromUserIdentity(userIdentity);

            if (user != null)
            {
                // we have an existing user, just log them in
                var auth = new HttpCookie(FormsAuthentication.FormsCookieName, GenerateTicket(user));
                Response.Cookies.Add(auth);
                return Redirect("/");
            }

            var username = j.profile.preferredUsername.ToString();
            var email = string.Empty;
            if (j.profile.email != null)
                email = j.profile.email.ToString();

            var u = new User
            {
                Id = Guid.NewGuid(),
                Identity = userIdentity,
                UserName = !string.IsNullOrEmpty(username) ? username : "******" + _userRepository.GetAll().Count(),
                Email = !string.IsNullOrEmpty(email) ? email : "*****@*****.**",
                Github = !string.IsNullOrEmpty(username) ? username : "",
                IsActive = true,
            };

            if (!_userRepository.GetAll().Any())
                _userRepository.AddRole(u, "Admin");

            if (j.profile.photo != null)
                u.AvatarUrl = j.profile.photo.ToString();

            _userRepository.Add(u);
            // TODO: add user to forms authentication
            // TODO: navigate them to /profile/edit

            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName,  GenerateTicket(u));

            //OM NOM NOM. ALL OF THE COOKIES
            Response.Cookies.Add(cookie);
            return Redirect("/");
        }
 public ActionResult User(User user)
 {
     return RedirectToAction("User", new { id = user.Id });
 }
        private string GenerateTicket(User user)
        {
            var newticket = new FormsAuthenticationTicket(
                1,
                user.UserName,
                DateTime.UtcNow,
                DateTime.UtcNow.AddMonths(1),
                true,
                user.Identity);

            return FormsAuthentication.Encrypt(newticket);
        }
        private static User NewUser(dynamic user)
        {
            var author = new User
            {
                Id = Guid.NewGuid(),
                UserName = user.name,
            };

            if (user.avatar_url != null)
            {
                string avatar = user.avatar_url;
                if (avatar.Contains("&"))
                    avatar = avatar.Substring(0, avatar.IndexOf("&"));
                author.AvatarUrl = avatar;
            }
            return author;
        }