Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.DocumentIntelligence" Version="1.0.0-beta.2" />
<PackageReference Include="Azure.Identity" Version="1.11.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Utility\Utility.csproj" />
</ItemGroup>

</Project>
49 changes: 49 additions & 0 deletions .NET(v4.0)/AuthenticateWithUserAssignedManagedIdentity/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// coding: utf - 8
// --------------------------------------------------------------------------
// Copyright(c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
// --------------------------------------------------------------------------
using Azure;
using Azure.AI.DocumentIntelligence;
using Azure.Identity;
using Utility;
namespace AuthenticateWithUserAssignedManagedIdentity
{
internal class Program
{
static async Task Main(string[] args)
{
// The endpoint to your Document Intelligence resource.
// To obtain the parameter, please reference the document
// in (..\..\Doc\Prerequisites For Authentication With User-Assigned Managed Identity.md)
var docIntelligenceEndpoint = "<Azure Document Intelligence Endpoint>";
var client = new DocumentIntelligenceClient(new Uri(docIntelligenceEndpoint), new DefaultAzureCredential());

// sample file online
var url = "https://documentintelligence.ai.azure.com/documents/samples/prebuilt/w2-single.png";
var content = new AnalyzeDocumentContent
{
UrlSource = new Uri(url)
};

// To see the list of all the supported fields returned by service and its corresponding types for each supported model,
// access https://aka.ms/di-prebuilt please.
Operation<AnalyzeResult> operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-tax.us.w2", content);
AnalyzeResult result = operation.Value;

// Extract DocumentField:
for (int i = 0; i < result.Documents.Count; i++)
{
Console.WriteLine($"Document {i}:");

AnalyzedDocument document = result.Documents[i];

foreach (var item in document.Fields)
{
Utils.ExtractValueFromDocumentField(item.Key, item.Value);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.DocumentIntelligence" Version="1.0.0-beta.2" />
<PackageReference Include="Azure.Identity" Version="1.11.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Utility\Utility.csproj" />
</ItemGroup>

</Project>
58 changes: 58 additions & 0 deletions .NET(v4.0)/DocumentIntelligenceWithServicePrincipal/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// coding: utf - 8
// --------------------------------------------------------------------------
// Copyright(c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
// --------------------------------------------------------------------------
using Azure;
using Azure.AI.DocumentIntelligence;
using Azure.Identity;
using Utility;

namespace AuthenticateWithServicePrincipal
{
internal class Program
{
static async Task Main(string[] args)
{
#region To obtain these parameters, please reference the document in (..\..\Doc\Prerequisites For Authentication With Service Principal.md)
// the endpoint to your Document Intelligence resource.
var docIntelligenceEndpoint = "<Azure Document Intelligence Endpoint>";
// your Azure tenant id.
var tenantId = "<Azure Tenant Id>";
// the Client Secrets of Microsoft Entra App.
var clientId = "<Id of Azure Entra App>";
// the Client Secrets of Microsoft Entra App.
var clientSecret = "<Secret of Azure Entra App>";
#endregion

var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var client = new DocumentIntelligenceClient(new Uri(docIntelligenceEndpoint), clientSecretCredential);

// sample file online
var url = "https://documentintelligence.ai.azure.com/documents/samples/prebuilt/w2-single.png";
var content = new AnalyzeDocumentContent
{
UrlSource = new Uri(url)
};

// To see the list of all the supported fields returned by service and its corresponding types for each supported model,
// access https://aka.ms/di-prebuilt please.
Operation<AnalyzeResult> operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-tax.us.w2", content);
AnalyzeResult result = operation.Value;

// Extract DocumentField:
for (int i = 0; i < result.Documents.Count; i++)
{
Console.WriteLine($"Document {i}:");

AnalyzedDocument document = result.Documents[i];

foreach (var item in document.Fields)
{
Utils.ExtractValueFromDocumentField(item.Key, item.Value);
}
}
}
}
}
1 change: 1 addition & 0 deletions .NET(v4.0)/Quickstarts/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using ConsoleTables;
using System.Configuration;
using System.Diagnostics;
using Utility;

namespace Quickstarts
{
Expand Down
4 changes: 4 additions & 0 deletions .NET(v4.0)/Quickstarts/Quickstarts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<Folder Include="Assets\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Utility\Utility.csproj" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy /Y &quot;$(ProjectDir)Assets\*.*&quot; &quot;$(TargetDir)Assets\&quot;" />
</Target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using Azure;
using Azure.AI.DocumentIntelligence;
using Utility;

namespace Quickstarts
{
Expand Down
1 change: 1 addition & 0 deletions .NET(v4.0)/Quickstarts/Samples/Sample_ExtractLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Azure;
using Azure.AI.DocumentIntelligence;
using ConsoleTables;
using Utility;

namespace Quickstarts
{
Expand Down
2 changes: 2 additions & 0 deletions .NET(v4.0)/Quickstarts/Samples/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// Licensed under the MIT License. See License.txt in the project root for
// license information.
// --------------------------------------------------------------------------
using Utility;

namespace Quickstarts
{
internal partial class Samples(string docIntelligenceEndPoint, string docIntelligenceApiKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// --------------------------------------------------------------------------
using Azure.AI.DocumentIntelligence;

namespace Quickstarts
namespace Utility
{
public static class SdkExtension
{
Expand Down
13 changes: 13 additions & 0 deletions .NET(v4.0)/Utility/Utility.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.DocumentIntelligence" Version="1.0.0-beta.2" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
// Licensed under the MIT License. See License.txt in the project root for
// license information.
// --------------------------------------------------------------------------

using Azure.AI.DocumentIntelligence;

namespace Quickstarts
namespace Utility
{
public class Utils
{
Expand Down
27 changes: 26 additions & 1 deletion .NET(v4.0)/sdk-samples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sample Code Snippet For Doc
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeSnippetForApiVer_2024-02-29_Preview", "CodeSnippetForApiVer_2024-02-29_Preview\CodeSnippetForApiVer_2024-02-29_Preview.csproj", "{EFCE60E7-6544-443C-8C68-53BFC9447B51}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeSnippetForApiVer_2023-10-31_Preview", "CodeSnippetForApiVer_2023-10-31_Preview\CodeSnippetForApiVer_2023-10-31_Preview.csproj", "{BC2D5C4C-C70E-4FB1-AD1E-1505F97FF231}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeSnippetForApiVer_2023-10-31_Preview", "CodeSnippetForApiVer_2023-10-31_Preview\CodeSnippetForApiVer_2023-10-31_Preview.csproj", "{BC2D5C4C-C70E-4FB1-AD1E-1505F97FF231}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Auth", "Auth", "{A216036E-43B5-4ABD-B4D4-6B2C5E1B6E7B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AuthenticateWithServicePrincipal", "DocumentIntelligenceWithServicePrincipal\AuthenticateWithServicePrincipal.csproj", "{4116A237-4981-4BF8-BE47-AC0A1CAB0EEC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common.Library", "Common.Library", "{50D0F7B2-DA35-4966-84EE-62B5002EA513}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utility", "Utility\Utility.csproj", "{CB79733C-D8D1-4603-A58A-E3BF0F27FB38}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AuthenticateWithUserAssignedManagedIdentity", "AuthenticateWithUserAssignedManagedIdentity\AuthenticateWithUserAssignedManagedIdentity.csproj", "{609B2281-6798-4D39-906D-51B29C0E3588}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -29,13 +39,28 @@ Global
{BC2D5C4C-C70E-4FB1-AD1E-1505F97FF231}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BC2D5C4C-C70E-4FB1-AD1E-1505F97FF231}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BC2D5C4C-C70E-4FB1-AD1E-1505F97FF231}.Release|Any CPU.Build.0 = Release|Any CPU
{4116A237-4981-4BF8-BE47-AC0A1CAB0EEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4116A237-4981-4BF8-BE47-AC0A1CAB0EEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4116A237-4981-4BF8-BE47-AC0A1CAB0EEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4116A237-4981-4BF8-BE47-AC0A1CAB0EEC}.Release|Any CPU.Build.0 = Release|Any CPU
{CB79733C-D8D1-4603-A58A-E3BF0F27FB38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CB79733C-D8D1-4603-A58A-E3BF0F27FB38}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CB79733C-D8D1-4603-A58A-E3BF0F27FB38}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CB79733C-D8D1-4603-A58A-E3BF0F27FB38}.Release|Any CPU.Build.0 = Release|Any CPU
{609B2281-6798-4D39-906D-51B29C0E3588}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{609B2281-6798-4D39-906D-51B29C0E3588}.Debug|Any CPU.Build.0 = Debug|Any CPU
{609B2281-6798-4D39-906D-51B29C0E3588}.Release|Any CPU.ActiveCfg = Release|Any CPU
{609B2281-6798-4D39-906D-51B29C0E3588}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{EFCE60E7-6544-443C-8C68-53BFC9447B51} = {E9CBB1EB-6D19-4981-A5F2-EFEA3CF0F0A7}
{BC2D5C4C-C70E-4FB1-AD1E-1505F97FF231} = {E9CBB1EB-6D19-4981-A5F2-EFEA3CF0F0A7}
{4116A237-4981-4BF8-BE47-AC0A1CAB0EEC} = {A216036E-43B5-4ABD-B4D4-6B2C5E1B6E7B}
{CB79733C-D8D1-4603-A58A-E3BF0F27FB38} = {50D0F7B2-DA35-4966-84EE-62B5002EA513}
{609B2281-6798-4D39-906D-51B29C0E3588} = {A216036E-43B5-4ABD-B4D4-6B2C5E1B6E7B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5CBC567B-C775-4E10-BCB4-E019111881C4}
Expand Down
31 changes: 31 additions & 0 deletions Doc/Prerequisites For Authentication With Service Principal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Prerequisites For Authentication With Service Principal

In this article, you'll learn how to create Azure AI Service, Microsoft Entra application and service principal that can be used with role-based access control (RBAC). When you register a new application in Microsoft Entra ID, a service principal is automatically created for the app registration. The service principal is the app's identity in the Microsoft Entra tenant. Access to resources is restricted by the roles assigned to the service principal, giving you control over which resources can be accessed and at which level.

## Create Azure AI Service
Create a [single-service](https://aka.ms/single-service) or [multi-service](https://aka.ms/multi-service) resource. You can use the free pricing tier (F0) to try the service and upgrade to a paid tier for production later.

## Register an application with Microsoft Entra ID and create a service principal
For this process, it could reference the [doc section](https://learn.microsoft.com/en-us/entra/identity-platform/howto-create-service-principal-portal#register-an-application-with-microsoft-entra-id-and-create-a-service-principal) completely.

## Assign a role to the application
1. Sign in to the [Azure portal](https://portal.azure.com/) with administrator privileges.
2. Select the Azure AI Service which created in previous steps.
3. Select **Access control (IAM)**.
4. Select **Add**, then select **Add role assignment**.
5. In the **Role** tab, select the **"Cognitive Services User"**
6. Select Next.
7. On the **Members** tab, for **Assign access to**, select **User, group, or service principal**.
8. Select **Select members**. By default, Microsoft Entra applications aren't displayed in the available options. To find your application, search for it by the name which register in previous steps.
9. Select the Select button, then select Review + assign.

## Set up authentication
Reference the [document](https://learn.microsoft.com/en-us/entra/identity-platform/howto-create-service-principal-portal#set-up-authentication), there're three options to set up authentication.
For the sample in repo, it's used [option 3](https://learn.microsoft.com/en-us/entra/identity-platform/howto-create-service-principal-portal#option-3-create-a-new-client-secret).

So far, all the parameters will be used in the sample programs, it could be obtained from above resouces.
1) DOCUMENTINTELLIGENCE_ENDPOINT - the endpoint to your Document Intelligence resource.
2) DOCUMENTINTELLIGENCE_TENANT_ID - your Azure tenant id.
3) DOCUMENTINTELLIGENCE_CLIENT_ID - the Application (client) ID of Microsoft Entra App.
4) DOCUMENTINTELLIGENCE_CLIENT_SECRET - the Client Secrets of Microsoft Entra App.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Prerequisites For Authentication With User-Assigned Managed Identity

In this article, you'll learn how to create Azure AI Service, and administrators can assign a role to an user-assigned managed identity.

## Create Azure AI Service
Create a [single-service](https://aka.ms/single-service) or [multi-service](https://aka.ms/multi-service) resource. You can use the free pricing tier (F0) to try the service and upgrade to a paid tier for production later.

## Assign A Role to User By Administrator
1. Sign in to the [Azure portal](https://portal.azure.com/) with administrator privileges.
2. Select the Azure AI Service which created in previous steps.
3. Select **Access control (IAM)**.
4. Select **Add**, then select **Add role assignment**.
5. In the **Role** tab, select the **"Cognitive Services User"**
6. Select Next.
7. On the **Members** tab, for **Assign access to**, select **User, group, or service principal**.
8. Select **Select members**, and choose the user who should have the role assigned.
9. Select the "Select" button, then select "Review + assign".

So far, the parameters will be used in the sample programs, it could be obtained from above resouces.
1) DOCUMENTINTELLIGENCE_ENDPOINT - the endpoint to your Document Intelligence resource.


Loading