diff --git a/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/App.config b/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/App.config index 0c258a2..86622cc 100644 --- a/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/App.config +++ b/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/App.config @@ -7,7 +7,7 @@ - + @@ -19,6 +19,26 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming.csproj b/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming.csproj index 9347d57..3bd5a30 100644 --- a/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming.csproj +++ b/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming.csproj @@ -34,6 +34,12 @@ 4 + + ..\packages\Azure.Core.1.4.1\lib\netstandard2.0\Azure.Core.dll + + + ..\packages\Azure.Search.Documents.11.1.1\lib\netstandard2.0\Azure.Search.Documents.dll + ..\packages\Emgu.CV.3.2.0.2721\lib\net35\Emgu.CV.UI.dll True @@ -42,9 +48,8 @@ ..\packages\Emgu.CV.3.2.0.2721\lib\net35\Emgu.CV.World.dll True - - ..\packages\Microsoft.Azure.Search.4.0.1-preview\lib\net452\Microsoft.Azure.Search.dll - True + + ..\packages\Microsoft.Bcl.AsyncInterfaces.1.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll ..\packages\Microsoft.Graph.1.5.1\lib\net45\Microsoft.Graph.dll @@ -72,12 +77,28 @@ True + + ..\packages\System.Buffers.4.5.0\lib\netstandard2.0\System.Buffers.dll + + + ..\packages\System.Diagnostics.DiagnosticSource.4.6.0\lib\net46\System.Diagnostics.DiagnosticSource.dll + + + ..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll + ..\packages\System.Net.Http.4.3.1\lib\net46\System.Net.Http.dll + + + ..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll + + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.6.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll + ..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net461\System.Security.Cryptography.Algorithms.dll @@ -90,6 +111,18 @@ ..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll + + ..\packages\System.Text.Encodings.Web.4.6.0\lib\netstandard2.0\System.Text.Encodings.Web.dll + + + ..\packages\System.Text.Json.4.6.0\lib\net461\System.Text.Json.dll + + + ..\packages\System.Threading.Tasks.Extensions.4.5.2\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll + + + ..\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll + diff --git a/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/Program.cs b/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/Program.cs index 618e83c..3c758e4 100644 --- a/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/Program.cs +++ b/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/Program.cs @@ -7,10 +7,12 @@ 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 { @@ -18,8 +20,8 @@ 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> _groupsCache = new ConcurrentDictionary>(); private static MicrosoftGraphHelper _microsoftGraphHelper; @@ -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"; @@ -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)) { @@ -138,7 +140,7 @@ private static async void RefreshCacheIfRequired(string user) _groupsCache[user] = groups; } } - + private static async void RefreshCache(IEnumerable users) { HttpClient client = new HttpClient(); @@ -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 results = _indexClient.Documents.Search("*", parameters); + SearchResults results = searchClient.Search("*", 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 groups) { - var actions = new IndexAction[] - { - 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 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"); @@ -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; } @@ -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() - }; + 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) { diff --git a/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/SecuredFiles.cs b/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/SecuredFiles.cs index e69067b..17cd343 100644 --- a/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/SecuredFiles.cs +++ b/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/SecuredFiles.cs @@ -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; } } \ No newline at end of file diff --git a/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/packages.config b/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/packages.config index 5e2df46..995c2f5 100644 --- a/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/packages.config +++ b/DotNetHowToSecurityTrimming/DotNetHowToSecurityTrimming/packages.config @@ -1,7 +1,9 @@  + + - + @@ -9,10 +11,19 @@ + + + + + + + + + \ No newline at end of file