簡體   English   中英

是否可以使用Azure AD身份驗證訪問SharePoint Online數據?

[英]Is It Possible To Access SharePoint Online Data Using Azure AD Authentication?

我在Azure App Service和桌面應用程序( D1 )上托管了一個自定義安全API( API1 ),該應用程序可以訪問自定義API( API1 )。

現在,我們需要從我們的自定義API( API1 )訪問SharePoint Online數據。

使用相同的桌面應用程序( D1 )從Azure App Service上托管的自定義API訪問SharePoint聯機數據是否可行?

當然可以! 但是要求是您的目錄中必須具有Sharepoint Online訂閱。

如何 :

  1. 將您的應用程序與AAD集成

  2. 為您的應用程序添加Office 365 Sharepoint Online API訪問權限。

  3. 為您的應用選擇必要的權限。

在此處輸入圖片說明

感謝Yang,您的建議很有用。

我已經執行了相同的步驟,並使用了下面的代碼,現在我可以使用Azure令牌從SPO網站獲取數據。

 static void ConnectToSPO()
    {
        string SiteURL = "https://SPOSite.sharepoint.com/";

        #region Obtain token
        AuthenticationResult result = null;
        // first, try to get a token silently
        try
        {
            result = authContext.AcquireTokenSilentAsync(SiteURL, clientId).Result;
        }
        catch (AggregateException exc)
        {
            AdalException ex = exc.InnerException as AdalException;

            // There is no token in the cache; prompt the user to sign-in.
            if (ex != null && ex.ErrorCode != "failed_to_acquire_token_silently")
            {
                // An unexpected error occurred.
                ShowError(ex);
                return;
            }
        }

        if (result == null)
        {
            UserCredential uc = TextualPrompt();
            // if you want to use Windows integrated auth, comment the line above and uncomment the one below
            // UserCredential uc = new UserCredential();
            try
            {
                result = authContext.AcquireTokenAsync(todoListResourceId, clientId, uc).Result;
            }
            catch (Exception ee)
            {
                ShowError(ee);
                return;
            }
        }

        #endregion

        #region Get SharePoint Online Context & Access SPO Data

        using (ClientContext ctx = TokenHelper.GetClientContextWithAccessToken(SiteURL, result.AccessToken))
        {

            try
            {

                Console.ForegroundColor = ConsoleColor.Yellow;

                Console.WriteLine("");
                Console.WriteLine("*****************************************************************************");
                Console.WriteLine("Connecting To SPO Site: " + SiteURL);

                ctx.Load(ctx.Web);
                ctx.ExecuteQuery();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Connected !");

                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Info: Site Name-> " + ctx.Web.Title);

                ctx.Load(ctx.Web.CurrentUser);
                ctx.ExecuteQuery();
                Console.WriteLine("Info: Current User Login Name-> " + ctx.Web.CurrentUser.LoginName);

                #region Read List Items
                Console.WriteLine("");
                Console.WriteLine("Info: Reading list items from list Test List");

                List testlist = ctx.Web.Lists.GetByTitle("Test List");
                CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
                ListItemCollection items = testlist.GetItems(query);
                ctx.Load(items);
                ctx.ExecuteQuery();
                foreach (ListItem listItem in items)
                {
                    // We have all the list item data. For example, Title. 
                    Console.WriteLine(listItem["Title"]);
                }

                Console.WriteLine("");
                #endregion
            }
            catch (Exception ex)
            {
                ShowError(ex);
            }
        } 
        #endregion
    }

請注意:

為了獲取Azure的訪問令牌,我使用了以下文章中的代碼。

主動目錄的dotnet本地,無頭

以下是我遵循的步驟:

  1. 遵循了文章中提到的步驟

  2. 為該應用程序添加了Office 365 Sharepoint Online API訪問權限。

  3. 為該應用選擇了必要的權限。

  4. 並使用上述代碼從SPO SIte檢索數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM