Microsoft Information Protection SDK - Quickstart voor het opnieuw publiceren van bestands-SDK (C#)
Overzicht
Voor een overzicht van dit scenario en waar het kan worden gebruikt, raadpleegt u Opnieuw publiceren in MIP SDK.
Vereisten
Als u dat nog niet hebt gedaan, moet u de volgende vereisten voltooien voordat u doorgaat:
- Volledige quickstart: Stel eerst vertrouwelijkheidslabels (C#) in, waarmee een eenvoudige Visual Studio-oplossing wordt gebouwd, om de vertrouwelijkheidslabels van een organisatie weer te geven, om vertrouwelijkheidslabels in te stellen en te lezen naar/van een bestand. Deze quickstart 'How to - Republish a protected file - C#' is gebaseerd op de vorige.
- Optioneel: Controleer bestandshandlers in de MIP SDK-concepten.
- Optioneel: Controleer beveiligingshandlers in de MIP SDK-concepten.
Logica toevoegen om een beveiligd bestand te bewerken en opnieuw te publiceren
Open de Visual Studio-oplossing die u hebt gemaakt in het vorige artikel 'Quickstart: Vertrouwelijkheidslabels instellen/ophalen(C#)'.
Open met Solution Explorer het .cs-bestand in uw project dat de implementatie van de
Main()
methode bevat. Deze wordt standaard ingesteld op dezelfde naam als het project dat het bevat, die u hebt opgegeven tijdens het maken van het project.Voeg aan het einde van de hoofdtekst, onder
Console.ReadKey()
en boven het afsluitblok van deMain()
toepassing (waar u was gebleven in de vorige quickstart), de volgende code in.
string protectedFilePath = "<protected-file-path>" // Originally protected file's path from previous quickstart.
//Create a fileHandler for consumption for the Protected File.
var protectedFileHandler = Task.Run(async () =>
await fileEngine.CreateFileHandlerAsync(protectedFilePath,// inputFilePath
protectedFilePath,// actualFilePath
false, //isAuditDiscoveryEnabled
null)).Result; // fileExecutionState
// Store protection handler from file
var protectionHandler = protectedFileHandler.Protection;
//Check if the user has the 'Edit' right to the file
if (protectionHandler.AccessCheck("Edit"))
{
// Decrypt file to temp path
var tempPath = Task.Run(async () => await protectedFileHandler.GetDecryptedTemporaryFileAsync()).Result;
/*
Your own application code to edit the decrypted file belongs here.
*/
/// Follow steps below for re-protecting the edited file. ///
// Create a new file handler using the temporary file path.
var republishHandler = Task.Run(async () => await fileEngine.CreateFileHandlerAsync(tempPath, tempPath, false)).Result;
// Set protection using the ProtectionHandler from the original consumption operation.
republishHandler.SetProtection(protectionHandler);
// New file path to save the edited file
string reprotectedFilePath = "<reprotected-file-path>" // New file path for saving reprotected file.
// Write changes
var reprotectedResult = Task.Run(async () => await republishHandler.CommitAsync(reprotectedFilePath)).Result;
var protectedLabel = protectedFileHandler.Label;
Console.WriteLine(string.Format("Originally protected file: {0}", protectedFilePath));
Console.WriteLine(string.Format("File LabelID: {0} \r\nProtectionOwner: {1} \r\nIsProtected: {2}",
protectedLabel.Label.Id,
protectedFileHandler.Protection.Owner,
protectedLabel.IsProtectionAppliedFromLabel.ToString()));
var reprotectedLabel = republishHandler.Label;
Console.WriteLine(string.Format("Reprotected file: {0}", reprotectedFilePath));
Console.WriteLine(string.Format("File LabelID: {0} \r\nProtectionOwner: {1} \r\nIsProtected: {2}",
reprotectedLabel.Label.Id,
republishHandler.Protection.Owner,
reprotectedLabel.IsProtectionAppliedFromLabel.ToString()));
Console.WriteLine("Press a key to continue.");
Console.ReadKey();
}
Zoek aan het einde van Main() het afsluitblok van de toepassing dat in de vorige quickstart is gemaakt en voeg onderstaande handlerregels toe om resources vrij te geven.
protectedFileHandler = null; protectionHandler = null;
Vervang de tijdelijke aanduidingen in de broncode met behulp van de volgende waarden:
Tijdelijke aanduiding Waarde <beveiligd-bestandspad> Beveiligd bestand uit de vorige quickstart. <opnieuw beveiligd-bestandspad> Het pad naar het uitvoerbestand om het gewijzigde bestand opnieuw te publiceren.
De toepassing bouwen en testen
Bouw en test uw clienttoepassing.
Gebruik Ctrl-Shift-B (build solution) om uw clienttoepassing te bouwen. Als u geen buildfouten hebt, gebruikt u F5 (Foutopsporing starten) om uw toepassing uit te voeren.
Als uw project wordt gebouwd en uitgevoerd, kan de toepassing telkens wanneer de SDK uw
AcquireToken()
methode aanroept, om verificatie vragen met behulp van Microsoft Authentication Library (MSAL). Als er al referenties in de cache aanwezig zijn, wordt u niet gevraagd u aan te melden en de lijst met labels te zien, gevolgd door de informatie over het toegepaste label en het gewijzigde bestand.
Personal : 73c47c6a-eb00-4a6a-8e19-efaada66dee6
Public : 73254501-3d5b-4426-979a-657881dfcb1e
General : da480625-e536-430a-9a9e-028d16a29c59
Confidential : 569af77e-61ea-4deb-b7e6-79dc73653959
Highly Confidential : 905845d6-b548-439c-9ce5-73b2e06be157
Press a key to continue.
Getting the label committed to file: C:\Test\Test_protected.docx
File Label: Confidential
IsProtected: True
Press a key to continue.
Originally protected file: C:\Test\Test_protected.docx
File LabelID: 569af77e-61ea-4deb-b7e6-79dc73653959
ProtectionOwner: User1@Contoso.OnMicrosoft.com
IsProtected: True
Reprotected file: C:\Test\Test_reprotected.docx
File LabelID: 569af77e-61ea-4deb-b7e6-79dc73653959
ProtectionOwner: User1@Contoso.OnMicrosoft.com
IsProtected: True
Press a key to continue.