Skip to content

Commit

Permalink
Merge pull request #48 from FrankieTF/Update-DotNetHowToSecurityTrimming
Browse files Browse the repository at this point in the history
Update DotNetHowToSecurityTrimming
  • Loading branch information
HeidiSteen authored Oct 20, 2020
2 parents 6637896 + 72bf1f2 commit 94f4908
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<add key="ClientId" value="[Your_Client_Id]" />
<add key="Tenant" value="[Your_Tenant]" />
<add key="SearchServiceApiKey" value="[Your_API_Key]" />
<add key="SearchServiceName" value="[Your_Service_Name]" />
<add key="SearchServiceEndPoint" value="[Your_Service_EndPoint]" />
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
Expand All @@ -19,6 +19,26 @@
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Azure.Core, Version=1.4.1.0, Culture=neutral, PublicKeyToken=92742159e12e44c8, processorArchitecture=MSIL">
<HintPath>..\packages\Azure.Core.1.4.1\lib\netstandard2.0\Azure.Core.dll</HintPath>
</Reference>
<Reference Include="Azure.Search.Documents, Version=11.1.1.0, Culture=neutral, PublicKeyToken=92742159e12e44c8, processorArchitecture=MSIL">
<HintPath>..\packages\Azure.Search.Documents.11.1.1\lib\netstandard2.0\Azure.Search.Documents.dll</HintPath>
</Reference>
<Reference Include="Emgu.CV.UI, Version=3.2.0.2721, Culture=neutral, PublicKeyToken=7281126722ab4438, processorArchitecture=MSIL">
<HintPath>..\packages\Emgu.CV.3.2.0.2721\lib\net35\Emgu.CV.UI.dll</HintPath>
<Private>True</Private>
Expand All @@ -42,9 +48,8 @@
<HintPath>..\packages\Emgu.CV.3.2.0.2721\lib\net35\Emgu.CV.World.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Search, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Azure.Search.4.0.1-preview\lib\net452\Microsoft.Azure.Search.dll</HintPath>
<Private>True</Private>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.1.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Graph, Version=1.5.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Graph.1.5.1\lib\net45\Microsoft.Graph.dll</HintPath>
Expand Down Expand Up @@ -72,12 +77,28 @@
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Diagnostics.DiagnosticSource, Version=4.0.4.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Diagnostics.DiagnosticSource.4.6.0\lib\net46\System.Diagnostics.DiagnosticSource.dll</HintPath>
</Reference>
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Net.Http.4.3.1\lib\net46\System.Net.Http.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.6.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Security.Cryptography.Algorithms, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net461\System.Security.Cryptography.Algorithms.dll</HintPath>
</Reference>
Expand All @@ -90,6 +111,18 @@
<Reference Include="System.Security.Cryptography.X509Certificates, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll</HintPath>
</Reference>
<Reference Include="System.Text.Encodings.Web, Version=4.0.4.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Encodings.Web.4.6.0\lib\netstandard2.0\System.Text.Encodings.Web.dll</HintPath>
</Reference>
<Reference Include="System.Text.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Json.4.6.0\lib\net461\System.Text.Json.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.2\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
105 changes: 50 additions & 55 deletions DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
using System.Linq;
using System.Net.Http;
using System.Threading;
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Newtonsoft.Json;
using Microsoft.Graph;
using Azure.Search.Documents;
using Azure.Search.Documents.Indexes;
using Azure;
using Azure.Search.Documents.Models;
using Azure.Search.Documents.Indexes.Models;

namespace DotNetHowToSecurityTrimming
{
static class Program
{
public static string ClientId;

private static ISearchServiceClient _searchClient;
private static ISearchIndexClient _indexClient;
private static SearchClient searchClient;
private static SearchIndexClient indexClient;

private static ConcurrentDictionary<string, List<string>> _groupsCache = new ConcurrentDictionary<string, List<string>>();
private static MicrosoftGraphHelper _microsoftGraphHelper;
Expand All @@ -33,9 +35,9 @@ static void Main(string[] args)
_microsoftGraphHelper = new MicrosoftGraphHelper(ClientId);
_microsoftGraphHelper.CreateGraphServiceClient();
string tenant = ConfigurationManager.AppSettings["Tenant"];

// Azure Search Initialization
string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
string searchServicEndpoint = ConfigurationManager.AppSettings["SearchServicEndpoint"];
string apiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"];
string indexName = "securedfiles";

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

// Create an HTTP reference to the catalog index
_searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));
_indexClient = new SearchIndexClient(searchServiceName, indexName, new SearchCredentials(apiKey));
indexClient = new SearchIndexClient(new Uri(searchServicEndpoint), new AzureKeyCredential(apiKey));
searchClient = indexClient.GetSearchClient(indexName);

if (DeleteIndex(indexName))
{
Expand Down Expand Up @@ -138,7 +140,7 @@ private static async void RefreshCacheIfRequired(string user)
_groupsCache[user] = groups;
}
}

private static async void RefreshCache(IEnumerable<User> users)
{
HttpClient client = new HttpClient();
Expand All @@ -151,59 +153,53 @@ private static void SearchQueryWithFilter(string user)
// Using the filter below, the search result will contain all documents that their GroupIds field contain any one of the
// Ids in the groups list
string filter = String.Format("groupIds/any(p:search.in(p, '{0}'))", string.Join(",", String.Join(",", _groupsCache[user])));
SearchParameters parameters =
new SearchParameters()
SearchOptions searchOptions =
new SearchOptions()
{
Filter = filter,
Select = new[] { "name" }
Filter = filter
};
searchOptions.Select.Add("name");

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

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

private static void IndexDocuments(string indexName, List<string> groups)
{
var actions = new IndexAction<SecuredFiles>[]
{
IndexAction.Upload(
new SecuredFiles()
{
FileId = "1",
Name = "secured_file_a",
GroupIds = new[] { groups[0] }
}),
IndexAction.Upload(
new SecuredFiles()
{
FileId = "2",
Name = "secured_file_b",
GroupIds = new[] { groups[0] }
}),
IndexAction.Upload(
new SecuredFiles()
{
FileId = "3",
Name = "secured_file_c",
GroupIds = new[] { groups[1] }
})
};

var batch = IndexBatch.New(actions);
IndexDocumentsBatch<SecuredFiles> batch = IndexDocumentsBatch.Create(
IndexDocumentsAction.Upload(
new SecuredFiles()
{
FileId = "1",
Name = "secured_file_a",
GroupIds = new[] { groups[0] }
}),
IndexDocumentsAction.Upload(
new SecuredFiles()
{
FileId = "2",
Name = "secured_file_b",
GroupIds = new[] { groups[0] }
}),
IndexDocumentsAction.Upload(
new SecuredFiles()
{
FileId = "3",
Name = "secured_file_c",
GroupIds = new[] { groups[1] }
}));

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

Console.WriteLine("Waiting for documents to be indexed...\n");
Expand All @@ -214,12 +210,12 @@ private static bool DeleteIndex(string indexName)
{
try
{
_searchClient.Indexes.Delete(indexName);
indexClient.DeleteIndex(indexName);
}
catch (Exception ex)
{
Console.WriteLine("Error deleting index: {0}\r\n", ex.Message);
Console.WriteLine("Did you remember to add your SearchServiceName and SearchServiceApiKey to the app.config?\r\n");
Console.WriteLine("Did you remember to add your SearchServicEndpoint and SearchServiceApiKey to the app.config?\r\n");
return false;
}

Expand All @@ -231,13 +227,12 @@ private static void CreateIndex(string indexName)
// Create the Azure Search index based on the included schema
try
{
var definition = new Index()
{
Name = indexName,
Fields = FieldBuilder.BuildForType<SecuredFiles>()
};
FieldBuilder fieldBuilder = new FieldBuilder();
var searchFields = fieldBuilder.Build(typeof(SecuredFiles));
var definition = new SearchIndex(indexName, searchFields);

indexClient.CreateOrUpdateIndex(definition);

_searchClient.Indexes.Create(definition);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
using System;
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Microsoft.Spatial;
using Newtonsoft.Json;
using Azure.Search.Documents.Indexes;
using System.Text.Json.Serialization;

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

[IsFilterable, IsSearchable, IsSortable]
[SimpleField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
public string Name { get; set; }

[IsFilterable]
[SimpleField(IsFilterable = true)]
public string[] GroupIds { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Azure.Core" version="1.4.1" targetFramework="net461" />
<package id="Azure.Search.Documents" version="11.1.1" targetFramework="net461" />
<package id="Emgu.CV" version="3.2.0.2721" targetFramework="net461" />
<package id="Microsoft.Azure.Search" version="4.0.1-preview" targetFramework="net461" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="1.0.0" targetFramework="net461" />
<package id="Microsoft.Graph" version="1.5.1" targetFramework="net461" />
<package id="Microsoft.Graph.Core" version="1.6.1" targetFramework="net461" />
<package id="Microsoft.Identity.Client" version="1.1.0-preview" targetFramework="net461" />
<package id="Microsoft.Rest.ClientRuntime" version="2.3.7" targetFramework="net461" />
<package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.6" targetFramework="net461" />
<package id="Microsoft.Spatial" version="7.2.0" targetFramework="net461" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net461" />
<package id="System.Buffers" version="4.5.0" targetFramework="net461" />
<package id="System.Diagnostics.DiagnosticSource" version="4.6.0" targetFramework="net461" />
<package id="System.Memory" version="4.5.3" targetFramework="net461" />
<package id="System.Net.Http" version="4.3.1" targetFramework="net461" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net461" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.6.0" targetFramework="net461" />
<package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.X509Certificates" version="4.3.0" targetFramework="net461" />
<package id="System.Text.Encodings.Web" version="4.6.0" targetFramework="net461" />
<package id="System.Text.Json" version="4.6.0" targetFramework="net461" />
<package id="System.Threading.Tasks.Extensions" version="4.5.2" targetFramework="net461" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net461" />
<package id="ZedGraph" version="5.1.5" targetFramework="net461" />
</packages>

0 comments on commit 94f4908

Please sign in to comment.