private async Task <ProfileDetails> TryRefreshToken(LiveIdAuth svc)
        {
            if (Request.Cookies["refresh_token"] != null && Request.Cookies["refresh_token"].Value.Length > 1)
            {
                var result = await svc.RefreshTokens();

                return(await UserFromToken(result, svc));
            }
            return(null);
        }
        protected async Task <ProfileDetails> TryAuthenticateFromHttpContext()
        {
            if (SessionWrapper.Get <ProfileDetails>("ProfileDetails") != null)
            {
                return(SessionWrapper.Get <ProfileDetails>("ProfileDetails"));
            }

            var svc = new LiveIdAuth();

            var result = await svc.Authenticate();

            string userId = null;

            if (result.Status != LiveConnectSessionStatus.Connected)
            {
                var resultstring = await svc.RefreshTokens();

                if (string.IsNullOrEmpty(resultstring))
                {
                    return(null);
                }
                var tokens = new { access_token = "", refresh_token = "" };
                var json   = JsonConvert.DeserializeAnonymousType(resultstring, tokens);
                userId = await svc.GetUserId(tokens.access_token);

                if (string.IsNullOrEmpty(userId))
                {
                    return(null);
                }
                return(await InitUserProfile(userId, json.access_token));
            }

            var     client     = new LiveConnectClient(result.Session);
            dynamic jsonResult = null;
            var     getResult  = await client.GetAsync("me");

            jsonResult = getResult.Result as dynamic;
            foreach (KeyValuePair <string, object> item in jsonResult)
            {
                if (item.Key.ToLower() == "id")
                {
                    userId = item.Value.ToString();
                }
            }
            //userId = jsonResult["id"].ToString();

            return(await InitUserProfile(userId, result.Session.AccessToken));
        }
        protected static async Task <ProfileDetails> ValidateAuthentication()
        {
            var svc   = new LiveIdAuth();
            var token = System.Web.HttpContext.Current.Request.Headers["LiveUserToken"];

            if (token == null)
            {
                var authCookie = System.Web.HttpContext.Current.Request.Cookies["access_token"];
                if (authCookie != null)
                {
                    token = authCookie.Value;
                }
            }
            var cachedProfile = ProfileCacheManager.GetProfileDetails(token);

            if (cachedProfile != null)
            {
                return(cachedProfile);
            }
            string userId = null;

            userId = await svc.GetUserId(token);

            if (userId == null)
            {
                var result = await svc.RefreshTokens();

                var tokens = new { access_token = "", refresh_token = "" };
                var json   = JsonConvert.DeserializeAnonymousType(result, tokens);
                userId = await svc.GetUserId(json.access_token);
            }
            if (userId == null || userId.Length <= 3)
            {
                return(null);
            }
            var profileService = DependencyResolver.Current.GetService(typeof(IProfileService)) as IProfileService;
            var profileDetails = profileService.GetProfile(userId);

            if (profileDetails != null && token != null)
            {
                ProfileCacheManager.CacheProfile(token, profileDetails);
            }

            return(profileDetails);
        }
        protected async Task <ProfileDetails> TryAuthenticateFromHttpContext()
        {
            if (Request.Headers.Get("host").Contains("localhost"))
            {
                return(null);
            }
            if (SessionWrapper.Get <ProfileDetails>("ProfileDetails") != null)
            {
                return(SessionWrapper.Get <ProfileDetails>("ProfileDetails"));
            }

            var svc = new LiveIdAuth();

            var profile = await TryRefreshToken(svc);

            if (profile != null)
            {
                return(profile);
            }
            var result = await svc.Authenticate();

            string userId = null;

            if (result.Status != LiveConnectSessionStatus.Connected)
            {
                return(await UserFromToken(await svc.RefreshTokens(), svc));
            }

            var     client     = new LiveConnectClient(result.Session);
            dynamic jsonResult = null;
            var     getResult  = await client.GetAsync("me");

            jsonResult = getResult.Result;
            foreach (KeyValuePair <string, object> item in jsonResult)
            {
                if (item.Key.ToLower() == "id")
                {
                    userId = item.Value.ToString();
                }
            }
            //userId = jsonResult["id"].ToString();

            return(await InitUserProfile(userId, result.Session.AccessToken));
        }
        protected async Task <ProfileDetails> TryAuthenticateFromAuthCode(string authCode)
        {
            var    svc    = new LiveIdAuth();
            string result = "";

            if (Request.Cookies["refresh_token"] != null && Request.Cookies["refresh_token"].Value.Length > 1)
            {
                result = await svc.RefreshTokens();
            }
            else
            {
                result = await svc.GetTokens(authCode);
            }
            var tokens = new { access_token = "", refresh_token = "" };
            var json   = JsonConvert.DeserializeAnonymousType(result, tokens);
            var userId = await svc.GetUserId(json.access_token);

            return(await InitUserProfile(userId, json.access_token));
        }