Exemplo n.º 1
0
        private void CheckForSession()
        {
            if (_session != null)
            {
                return;
            }
            var access = Repository.Queryable <MemberStorageAccess>()
                         .FirstOrDefault(
                s => s.MemberId == CurrentMemberId && (int)s.Type == (int)StorageProviderType.OneDrive);

            if (access == null)
            {
                throw new InvalidCredentialException("Accesstoken could not be found");
            }

            var restClient = new RestClient("https://login.live.com/oauth20_token.srf");
            var request    = new RestRequest();

            request.Method = Method.POST;
            request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            var body =
                string.Format(
                    "client_id={0}&redirect_uri={1}&client_secret={2}&refresh_token={3}&grant_type=refresh_token"
                    , Startup.MicrosoftClientId, RedirectUrl, Startup.MicrosoftClientSecret, access.Token);

            request.AddParameter("application/x-www-form-urlencoded", body, ParameterType.RequestBody);

            var     result = restClient.Execute(request);
            dynamic token  = JsonConvert.DeserializeObject(result.Content);

            access.Token = token.refresh_token;
            string userId = token.user_id;

            Repository.Update(access);

            //TODO: Hier muss noch überprüft werden, wie mit eventuellen Timeouts umgegangen wird
            //falls der Benutzer sich tage/wochenlange nicht anmeldet und Synchronisiert
            _liveAuthClient = new LiveAuthClient(Startup.MicrosoftClientId, Startup.MicrosoftClientSecret, RedirectUrl,
                                                 new TokenHandler(access.Token, userId));

            var authorizeResult = _liveAuthClient.InitializeSessionAsync(RedirectUrl).Result;

            if (authorizeResult.Status != LiveConnectSessionStatus.Connected)
            {
                throw new InvalidCredentialException("One Drive cannot be authorized");
            }

            _session = _liveAuthClient.Session;

            GetFolderIds();
        }