简体   繁体   English

是否可以使用Azure AD身份验证访问SharePoint Online数据?

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

I have a custom Secure API ( API1 ) hosted on Azure App Service & a desktop application( D1 ) which can access the custom API ( API1 ). 我在Azure App Service和桌面应用程序( D1 )上托管了一个自定义安全API( API1 ),该应用程序可以访问自定义API( API1 )。

Now, we have a requirement to access the SharePoint Online data from our custom API ( API1 ). 现在,我们需要从我们的自定义API( API1 )访问SharePoint Online数据。

Is this feasible, to access SharePoint online data from custom API hosted on Azure App Service using same desktop application ( D1 ) ? 使用相同的桌面应用程序( D1 )从Azure App Service上托管的自定义API访问SharePoint联机数据是否可行?

Of course you can! 当然可以! But the the requirement is that you have Sharepoint Online subscription in your Directory. 但是要求是您的目录中必须具有Sharepoint Online订阅。

How to : 如何 :

  1. Integrate your App with AAD . 将您的应用程序与AAD集成

  2. Add Office 365 Sharepoint Online API access permissions for your App. 为您的应用程序添加Office 365 Sharepoint Online API访问权限。

  3. Select necessary permissions for your App. 为您的应用选择必要的权限。

在此处输入图片说明

Thanks Yang , your suggestion was useful. 感谢Yang,您的建议很有用。

I have followed the same steps and used the below code and now I am able to get the data from SPO Site using Azure Token. 我已经执行了相同的步骤,并使用了下面的代码,现在我可以使用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
    }

Please Note: 请注意:

To Get the Access Token for Azure , I have used code from below article. 为了获取Azure的访问令牌,我使用了以下文章中的代码。

active-directory-dotnet-native-headless 主动目录的dotnet本地,无头

Below are the steps I have followed: 以下是我遵循的步骤:

  1. Followed the steps mentioned in the article 遵循了文章中提到的步骤

  2. Added Office 365 Sharepoint Online API access permissions for the App. 为该应用程序添加了Office 365 Sharepoint Online API访问权限。

  3. Selected necessary permissions for the App. 为该应用选择了必要的权限。

  4. And used the code mentioned above to retrieve data from SPO SIte. 并使用上述代码从SPO SIte检索数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure AD安全组对SharePoint Online的访问 - Azure AD Security Group access to SharePoint Online 将IP地址身份验证添加到Windows Azure AD或SharePoint Online? - Add IP Address Authentication to Windows Azure AD or SharePoint Online? 使用 Azure AD v2.0 身份验证的 Sharepoint Online REST API - Sharepoint Online REST API with Azure AD v2.0 authentication 如何使用 v1 Azure AD 应用程序和客户端凭据访问 Sharepoint 在线 API - How to Access Sharepoint Online API with v1 Azure AD Application and Client Credentials SharePoint 2013的Azure AD身份验证不起作用 - Azure AD Authentication with SharePoint 2013 Not Working 如何通过 AD 身份验证使用 Azure API 格式访问 Application Insights 数据 - How to access Application Insights data using the Azure API format with AD authentication 使用 Azure 数据工厂将 Sharepoint 在线 csv 复制到数据湖 - Copy Sharepoint online csv to datalake using Azure Data Factory 有没有一种方法可以在不使用AD身份验证的情况下访问Azure密钥库? - Is there a way to access azure key vault without using AD authentication? Blazor 使用 Azure AD 身份验证允许匿名访问 - Blazor using Azure AD authentication allowing anonymous access 用于数据仓库身份验证的Azure AD - Azure AD for authentication with Data Warehouse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM