public async Task <ActionResult> Index()
        {
            string expectedTokenId = this.HttpContext.Session.GetString("tvId");
            string tokenId         = this.HttpContext.Request.Query["tokenId"];

            if (String.IsNullOrWhiteSpace(tokenId) || tokenId != expectedTokenId)
            {
                // Call is coming from a different session, so it will not be allowed
                throw new InvalidOperationException("token ID does not match expected value, will not save");
            }

            // Ensure we got a code back; otherwise auth flow didn't complete successfully
            string code = this.HttpContext.Request.Query["code"];

            if (!String.IsNullOrWhiteSpace(code))
            {
                // Set up Token Store client
                var    azureServiceTokenProvider = new AzureServiceTokenProvider();
                string tokenStoreApiToken        = await azureServiceTokenProvider.GetAccessTokenAsync("https://tokenstore.azure.net");

                string tokenStoreUrl    = this._configuration["TokenStoreUrl"];
                var    tokenStoreClient = new TokenStore.TokenStoreClient(tokenStoreUrl, tokenStoreApiToken);

                // Call "save" on Token Store to verify the auth flow and finalize the token
                string serviceId = this.HttpContext.Request.Query["serviceId"];
                await tokenStoreClient.SaveTokenAsync(serviceId, tokenId, code);
            }

            return(this.RedirectToPage("/Index"));
        }
Exemple #2
0
        private async Task <TokenStore.Token> GetOrCreateTokenResourceAsync(TokenStore.TokenStoreClient client, string serviceId, string tokenId)
        {
            var retrievedToken = await client.GetTokenResourceAsync(serviceId, tokenId);

            if (retrievedToken != null)
            {
                return(retrievedToken);
            }

            return(await client.CreateTokenResourceAsync(serviceId, tokenId));
        }
Exemple #3
0
        public async Task OnGetAsync()
        {
            // Ensure that user is authenticated
            this.LoggedIn = this.User.Identity.IsAuthenticated;
            if (!this.LoggedIn)
            {
                return;
            }

            this.UserName = this.User.FindFirst("name").Value;
            var objectId = this.User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

            // Get an API token to access Token Store
            // For local debug use RunAs=Developer to get User logged into VS
            // var azureServiceTokenProvider = new AzureServiceTokenProvider("RunAs=Developer; DeveloperTool=VisualStudio");
            var azureServiceTokenProvider = new AzureServiceTokenProvider("");

            var tokenStoreUrl = this._configuration["TokenStoreUrl"];

            if (tokenStoreUrl.EndsWith("/"))
            {
                tokenStoreUrl = tokenStoreUrl.TrimEnd('/');
            }

            var tokenStoreApiToken = await azureServiceTokenProvider.GetAccessTokenAsync(tokenStoreUrl);

            var tokenStoreClient = new TokenStore.TokenStoreClient(tokenStoreUrl, tokenStoreApiToken);

            // Get Token Store token resource for Dropbox for this user (and create it if it doesn't exist)
            var tokenStoreDropboxToken = await GetOrCreateTokenResourceAsync(tokenStoreClient, "dropbox", objectId);

            // Check Dropbox token status and set in view data
            this.DropboxData.IsConnected = tokenStoreDropboxToken.Status.State.ToLower() == "ok";

            // If connected, get data from Dropbox and set in view data
            if (this.DropboxData.IsConnected)
            {
                this.DropboxData.Files = await GetDropboxDocumentsAsync(tokenStoreDropboxToken.Value.AccessToken);
            }
            // Otherwise, set Dropbox login URI in view data
            else
            {
                var postAuthRedirectUrl = GetPostAuthRedirectUrl("dropbox", objectId);
                this.DropboxData.LoginUrl = $"{tokenStoreDropboxToken.LoginUri}?PostLoginRedirectUrl={Uri.EscapeDataString(postAuthRedirectUrl)}";
            }



            // Get Token Store token resource for Graph for this user (and create it if it doesn't exist)
            var tokenStoreGraphToken = await GetOrCreateTokenResourceAsync(tokenStoreClient, "graph", objectId);

            // Check Graph token status and set in view data
            this.GraphData.IsConnected = tokenStoreGraphToken.Status.State.ToLower() == "ok";

            // If connected, get data from Graph and set in view data
            if (this.GraphData.IsConnected)
            {
                this.GraphData.Files = await GetGraphDocumentsAsync(tokenStoreGraphToken.Value.AccessToken);
            }
            // Otherwise, set Graph login URI in view data
            else
            {
                var redirectUrl = GetPostAuthRedirectUrl("graph", objectId);
                this.GraphData.LoginUrl = $"{tokenStoreGraphToken.LoginUri}?PostLoginRedirectUrl={Uri.EscapeDataString(redirectUrl)}";
            }



            // Associate token name with this session, so that the post-auth handler can verify where the login flow originated
            this.HttpContext.Session.SetString("tvId", objectId);
        }