Skip to content

Commit 94f4908

Browse files
authored
Merge pull request #48 from FrankieTF/Update-DotNetHowToSecurityTrimming
Update DotNetHowToSecurityTrimming
2 parents 6637896 + 72bf1f2 commit 94f4908

File tree

5 files changed

+124
-73
lines changed

5 files changed

+124
-73
lines changed

DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/App.config

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<add key="ClientId" value="[Your_Client_Id]" />
88
<add key="Tenant" value="[Your_Tenant]" />
99
<add key="SearchServiceApiKey" value="[Your_API_Key]" />
10-
<add key="SearchServiceName" value="[Your_Service_Name]" />
10+
<add key="SearchServiceEndPoint" value="[Your_Service_EndPoint]" />
1111
</appSettings>
1212
<runtime>
1313
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
@@ -19,6 +19,26 @@
1919
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
2020
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
2121
</dependentAssembly>
22+
<dependentAssembly>
23+
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
24+
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
25+
</dependentAssembly>
26+
<dependentAssembly>
27+
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
28+
<bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0" />
29+
</dependentAssembly>
30+
<dependentAssembly>
31+
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
32+
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
33+
</dependentAssembly>
34+
<dependentAssembly>
35+
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
36+
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
37+
</dependentAssembly>
38+
<dependentAssembly>
39+
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
40+
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
41+
</dependentAssembly>
2242
</assemblyBinding>
2343
</runtime>
2444
</configuration>

DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming.csproj

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
<WarningLevel>4</WarningLevel>
3535
</PropertyGroup>
3636
<ItemGroup>
37+
<Reference Include="Azure.Core, Version=1.4.1.0, Culture=neutral, PublicKeyToken=92742159e12e44c8, processorArchitecture=MSIL">
38+
<HintPath>..\packages\Azure.Core.1.4.1\lib\netstandard2.0\Azure.Core.dll</HintPath>
39+
</Reference>
40+
<Reference Include="Azure.Search.Documents, Version=11.1.1.0, Culture=neutral, PublicKeyToken=92742159e12e44c8, processorArchitecture=MSIL">
41+
<HintPath>..\packages\Azure.Search.Documents.11.1.1\lib\netstandard2.0\Azure.Search.Documents.dll</HintPath>
42+
</Reference>
3743
<Reference Include="Emgu.CV.UI, Version=3.2.0.2721, Culture=neutral, PublicKeyToken=7281126722ab4438, processorArchitecture=MSIL">
3844
<HintPath>..\packages\Emgu.CV.3.2.0.2721\lib\net35\Emgu.CV.UI.dll</HintPath>
3945
<Private>True</Private>
@@ -42,9 +48,8 @@
4248
<HintPath>..\packages\Emgu.CV.3.2.0.2721\lib\net35\Emgu.CV.World.dll</HintPath>
4349
<Private>True</Private>
4450
</Reference>
45-
<Reference Include="Microsoft.Azure.Search, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
46-
<HintPath>..\packages\Microsoft.Azure.Search.4.0.1-preview\lib\net452\Microsoft.Azure.Search.dll</HintPath>
47-
<Private>True</Private>
51+
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
52+
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.1.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
4853
</Reference>
4954
<Reference Include="Microsoft.Graph, Version=1.5.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
5055
<HintPath>..\packages\Microsoft.Graph.1.5.1\lib\net45\Microsoft.Graph.dll</HintPath>
@@ -72,12 +77,28 @@
7277
<Private>True</Private>
7378
</Reference>
7479
<Reference Include="System" />
80+
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
81+
<HintPath>..\packages\System.Buffers.4.5.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
82+
</Reference>
7583
<Reference Include="System.ComponentModel.DataAnnotations" />
7684
<Reference Include="System.Configuration" />
7785
<Reference Include="System.Core" />
86+
<Reference Include="System.Diagnostics.DiagnosticSource, Version=4.0.4.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
87+
<HintPath>..\packages\System.Diagnostics.DiagnosticSource.4.6.0\lib\net46\System.Diagnostics.DiagnosticSource.dll</HintPath>
88+
</Reference>
89+
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
90+
<HintPath>..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll</HintPath>
91+
</Reference>
7892
<Reference Include="System.Net.Http, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
7993
<HintPath>..\packages\System.Net.Http.4.3.1\lib\net46\System.Net.Http.dll</HintPath>
8094
</Reference>
95+
<Reference Include="System.Numerics" />
96+
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
97+
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
98+
</Reference>
99+
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
100+
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.6.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
101+
</Reference>
81102
<Reference Include="System.Security.Cryptography.Algorithms, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
82103
<HintPath>..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net461\System.Security.Cryptography.Algorithms.dll</HintPath>
83104
</Reference>
@@ -90,6 +111,18 @@
90111
<Reference Include="System.Security.Cryptography.X509Certificates, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
91112
<HintPath>..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll</HintPath>
92113
</Reference>
114+
<Reference Include="System.Text.Encodings.Web, Version=4.0.4.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
115+
<HintPath>..\packages\System.Text.Encodings.Web.4.6.0\lib\netstandard2.0\System.Text.Encodings.Web.dll</HintPath>
116+
</Reference>
117+
<Reference Include="System.Text.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
118+
<HintPath>..\packages\System.Text.Json.4.6.0\lib\net461\System.Text.Json.dll</HintPath>
119+
</Reference>
120+
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
121+
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.2\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
122+
</Reference>
123+
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
124+
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll</HintPath>
125+
</Reference>
93126
<Reference Include="System.Xml.Linq" />
94127
<Reference Include="System.Data.DataSetExtensions" />
95128
<Reference Include="Microsoft.CSharp" />

DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/Program.cs

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@
77
using System.Linq;
88
using System.Net.Http;
99
using System.Threading;
10-
using Microsoft.Azure.Search;
11-
using Microsoft.Azure.Search.Models;
12-
using Newtonsoft.Json;
1310
using Microsoft.Graph;
11+
using Azure.Search.Documents;
12+
using Azure.Search.Documents.Indexes;
13+
using Azure;
14+
using Azure.Search.Documents.Models;
15+
using Azure.Search.Documents.Indexes.Models;
1416

1517
namespace DotNetHowToSecurityTrimming
1618
{
1719
static class Program
1820
{
1921
public static string ClientId;
2022

21-
private static ISearchServiceClient _searchClient;
22-
private static ISearchIndexClient _indexClient;
23+
private static SearchClient searchClient;
24+
private static SearchIndexClient indexClient;
2325

2426
private static ConcurrentDictionary<string, List<string>> _groupsCache = new ConcurrentDictionary<string, List<string>>();
2527
private static MicrosoftGraphHelper _microsoftGraphHelper;
@@ -33,9 +35,9 @@ static void Main(string[] args)
3335
_microsoftGraphHelper = new MicrosoftGraphHelper(ClientId);
3436
_microsoftGraphHelper.CreateGraphServiceClient();
3537
string tenant = ConfigurationManager.AppSettings["Tenant"];
36-
38+
3739
// Azure Search Initialization
38-
string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
40+
string searchServicEndpoint = ConfigurationManager.AppSettings["SearchServicEndpoint"];
3941
string apiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"];
4042
string indexName = "securedfiles";
4143

@@ -50,8 +52,8 @@ static void Main(string[] args)
5052
RefreshCache(users);
5153

5254
// Create an HTTP reference to the catalog index
53-
_searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));
54-
_indexClient = new SearchIndexClient(searchServiceName, indexName, new SearchCredentials(apiKey));
55+
indexClient = new SearchIndexClient(new Uri(searchServicEndpoint), new AzureKeyCredential(apiKey));
56+
searchClient = indexClient.GetSearchClient(indexName);
5557

5658
if (DeleteIndex(indexName))
5759
{
@@ -138,7 +140,7 @@ private static async void RefreshCacheIfRequired(string user)
138140
_groupsCache[user] = groups;
139141
}
140142
}
141-
143+
142144
private static async void RefreshCache(IEnumerable<User> users)
143145
{
144146
HttpClient client = new HttpClient();
@@ -151,59 +153,53 @@ private static void SearchQueryWithFilter(string user)
151153
// Using the filter below, the search result will contain all documents that their GroupIds field contain any one of the
152154
// Ids in the groups list
153155
string filter = String.Format("groupIds/any(p:search.in(p, '{0}'))", string.Join(",", String.Join(",", _groupsCache[user])));
154-
SearchParameters parameters =
155-
new SearchParameters()
156+
SearchOptions searchOptions =
157+
new SearchOptions()
156158
{
157-
Filter = filter,
158-
Select = new[] { "name" }
159+
Filter = filter
159160
};
161+
searchOptions.Select.Add("name");
160162

161-
DocumentSearchResult<SecuredFiles> results = _indexClient.Documents.Search<SecuredFiles>("*", parameters);
163+
SearchResults<SecuredFiles> results = searchClient.Search<SecuredFiles>("*", searchOptions);
162164

163-
Console.WriteLine("Results for groups '{0}' : {1}", _groupsCache[user], results.Results.Select(r => r.Document.Name));
165+
Console.WriteLine("Results for groups '{0}' : {1}", _groupsCache[user], results.GetResults().Select(r => r.Document.Name));
164166
}
165167

166168
private static void IndexDocuments(string indexName, List<string> groups)
167169
{
168-
var actions = new IndexAction<SecuredFiles>[]
169-
{
170-
IndexAction.Upload(
171-
new SecuredFiles()
172-
{
173-
FileId = "1",
174-
Name = "secured_file_a",
175-
GroupIds = new[] { groups[0] }
176-
}),
177-
IndexAction.Upload(
178-
new SecuredFiles()
179-
{
180-
FileId = "2",
181-
Name = "secured_file_b",
182-
GroupIds = new[] { groups[0] }
183-
}),
184-
IndexAction.Upload(
185-
new SecuredFiles()
186-
{
187-
FileId = "3",
188-
Name = "secured_file_c",
189-
GroupIds = new[] { groups[1] }
190-
})
191-
};
192-
193-
var batch = IndexBatch.New(actions);
170+
IndexDocumentsBatch<SecuredFiles> batch = IndexDocumentsBatch.Create(
171+
IndexDocumentsAction.Upload(
172+
new SecuredFiles()
173+
{
174+
FileId = "1",
175+
Name = "secured_file_a",
176+
GroupIds = new[] { groups[0] }
177+
}),
178+
IndexDocumentsAction.Upload(
179+
new SecuredFiles()
180+
{
181+
FileId = "2",
182+
Name = "secured_file_b",
183+
GroupIds = new[] { groups[0] }
184+
}),
185+
IndexDocumentsAction.Upload(
186+
new SecuredFiles()
187+
{
188+
FileId = "3",
189+
Name = "secured_file_c",
190+
GroupIds = new[] { groups[1] }
191+
}));
194192

195193
try
196194
{
197-
_indexClient.Documents.Index(batch);
195+
IndexDocumentsResult result = searchClient.IndexDocuments(batch);
198196
}
199-
catch (IndexBatchException e)
197+
catch (Exception)
200198
{
201199
// Sometimes when your Search service is under load, indexing will fail for some of the documents in
202200
// the batch. Depending on your application, you can take compensating actions like delaying and
203201
// retrying. For this simple demo, we just log the failed document keys and continue.
204-
Console.WriteLine(
205-
"Failed to index some of the documents: {0}",
206-
String.Join(", ", e.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key)));
202+
Console.WriteLine("Failed to index some of the documents: {0}");
207203
}
208204

209205
Console.WriteLine("Waiting for documents to be indexed...\n");
@@ -214,12 +210,12 @@ private static bool DeleteIndex(string indexName)
214210
{
215211
try
216212
{
217-
_searchClient.Indexes.Delete(indexName);
213+
indexClient.DeleteIndex(indexName);
218214
}
219215
catch (Exception ex)
220216
{
221217
Console.WriteLine("Error deleting index: {0}\r\n", ex.Message);
222-
Console.WriteLine("Did you remember to add your SearchServiceName and SearchServiceApiKey to the app.config?\r\n");
218+
Console.WriteLine("Did you remember to add your SearchServicEndpoint and SearchServiceApiKey to the app.config?\r\n");
223219
return false;
224220
}
225221

@@ -231,13 +227,12 @@ private static void CreateIndex(string indexName)
231227
// Create the Azure Search index based on the included schema
232228
try
233229
{
234-
var definition = new Index()
235-
{
236-
Name = indexName,
237-
Fields = FieldBuilder.BuildForType<SecuredFiles>()
238-
};
230+
FieldBuilder fieldBuilder = new FieldBuilder();
231+
var searchFields = fieldBuilder.Build(typeof(SecuredFiles));
232+
var definition = new SearchIndex(indexName, searchFields);
233+
234+
indexClient.CreateOrUpdateIndex(definition);
239235

240-
_searchClient.Indexes.Create(definition);
241236
}
242237
catch (Exception ex)
243238
{
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
using System;
2-
using Microsoft.Azure.Search;
3-
using Microsoft.Azure.Search.Models;
4-
using Microsoft.Spatial;
5-
using Newtonsoft.Json;
1+
using Azure.Search.Documents.Indexes;
2+
using System.Text.Json.Serialization;
63

7-
// The SerializePropertyNamesAsCamelCase attribute is defined in the Azure Search .NET SDK.
8-
// It ensures that Pascal-case property names in the model class are mapped to camel-case
9-
// field names in the index.
10-
[SerializePropertyNamesAsCamelCase]
114
public partial class SecuredFiles
125
{
13-
[System.ComponentModel.DataAnnotations.Key]
14-
[IsFilterable]
6+
[SimpleField(IsKey = true, IsFilterable = true)]
157
public string FileId { get; set; }
168

17-
[IsFilterable, IsSearchable, IsSortable]
9+
[SimpleField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
1810
public string Name { get; set; }
1911

20-
[IsFilterable]
12+
[SimpleField(IsFilterable = true)]
2113
public string[] GroupIds { get; set; }
2214
}
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Azure.Core" version="1.4.1" targetFramework="net461" />
4+
<package id="Azure.Search.Documents" version="11.1.1" targetFramework="net461" />
35
<package id="Emgu.CV" version="3.2.0.2721" targetFramework="net461" />
4-
<package id="Microsoft.Azure.Search" version="4.0.1-preview" targetFramework="net461" />
6+
<package id="Microsoft.Bcl.AsyncInterfaces" version="1.0.0" targetFramework="net461" />
57
<package id="Microsoft.Graph" version="1.5.1" targetFramework="net461" />
68
<package id="Microsoft.Graph.Core" version="1.6.1" targetFramework="net461" />
79
<package id="Microsoft.Identity.Client" version="1.1.0-preview" targetFramework="net461" />
810
<package id="Microsoft.Rest.ClientRuntime" version="2.3.7" targetFramework="net461" />
911
<package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.6" targetFramework="net461" />
1012
<package id="Microsoft.Spatial" version="7.2.0" targetFramework="net461" />
1113
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net461" />
14+
<package id="System.Buffers" version="4.5.0" targetFramework="net461" />
15+
<package id="System.Diagnostics.DiagnosticSource" version="4.6.0" targetFramework="net461" />
16+
<package id="System.Memory" version="4.5.3" targetFramework="net461" />
1217
<package id="System.Net.Http" version="4.3.1" targetFramework="net461" />
18+
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net461" />
19+
<package id="System.Runtime.CompilerServices.Unsafe" version="4.6.0" targetFramework="net461" />
1320
<package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net461" />
1421
<package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net461" />
1522
<package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net461" />
1623
<package id="System.Security.Cryptography.X509Certificates" version="4.3.0" targetFramework="net461" />
24+
<package id="System.Text.Encodings.Web" version="4.6.0" targetFramework="net461" />
25+
<package id="System.Text.Json" version="4.6.0" targetFramework="net461" />
26+
<package id="System.Threading.Tasks.Extensions" version="4.5.2" targetFramework="net461" />
27+
<package id="System.ValueTuple" version="4.5.0" targetFramework="net461" />
1728
<package id="ZedGraph" version="5.1.5" targetFramework="net461" />
1829
</packages>

0 commit comments

Comments
 (0)