Example #1
0
        protected static async Task <ProfileDetails> ValidateAuthentication()
        {
            var svc   = new LiveIdAuth();
            var token = System.Web.HttpContext.Current.Request.Headers["LiveUserToken"];

            if (token == null)
            {
                token = System.Web.HttpContext.Current.Request.QueryString["LiveUserToken"];
            }
            var cachedProfile = ProfileCacheManager.GetProfileDetails(token);

            if (cachedProfile != null)
            {
                return(cachedProfile);
            }
            var userId = await svc.GetUserId(token);

            if (userId != null && userId.Length > 3)
            {
                var profileService = DependencyResolver.Current.GetService(typeof(IProfileService)) as IProfileService;
                var profileDetails = profileService.GetProfile(userId);
                if (profileDetails != null)
                {
                    ProfileCacheManager.CacheProfile(token, profileDetails);
                }

                return(profileDetails);
            }

            return(null);
        }
        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 (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));
        }
        private async Task <ProfileDetails> UserFromToken(string tokenResult, LiveIdAuth svc)
        {
            if (string.IsNullOrEmpty(tokenResult))
            {
                return(null);
            }
            var tokens = new { access_token = "", refresh_token = "" };
            var json   = JsonConvert.DeserializeAnonymousType(tokenResult, tokens);
            var userId = await svc.GetUserId(json.access_token);

            if (string.IsNullOrEmpty(userId))
            {
                return(null);
            }
            return(await InitUserProfile(userId, json.access_token));
        }
        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));
        }