Schnellstart: Clientanwendungsinitialisierung – Policy SDK (C#)

Diese Schnellstartanleitung zeigt Ihnen, wie Sie zur Laufzeit das Client-Initialisierungsmuster implementieren, das vom MIP SDK .NET-Wrapper für das Policy SDK verwendet wird.

Hinweis

Jede Clientanwendung, die das MIP-.NET-Richtlinien-SDK verwendet, benötigt die Schritte in dieser Schnellstartanleitung. Schließen Sie das Einrichten und Konfigurieren des MIP-SDKs vor dem Start ab.

Voraussetzungen

Sollten Sie das noch nicht getan haben, sorgen Sie dafür, dass Sie:

Visual Studio-Lösung und Projekt erstellen

  1. Öffnen Sie Visual Studio 2022 oder höher, wählen Sie das Menü "Datei ", "Neu", "Projekt" aus.

    • Wählen Sie "Konsolen-App" aus (für .NET 8 oder höher).
    • Geben Sie einen Namen und einen Speicherort für das Projekt an.
  2. Fügen Sie die NuGet-Pakete für das MIP-Richtlinien-SDK und MSAL hinzu:

    • Klicken Sie in Projektmappen-Explorer mit der rechten Maustaste auf den Projektknoten, und wählen Sie "NuGet-Pakete verwalten" aus.
    • Wählen Sie "Durchsuchen" aus, geben Sie "Microsoft.InformationProtection" in das Suchfeld ein, und installieren Sie das Microsoft.InformationProtection.Policy-Paket .
    • Suchen und installieren Sie Microsoft.Identity.Client.

Implementieren eines Authentifizierungsdelegats

  1. Hinzufügen einer neuen Klasse mit dem Namen AuthDelegateImplementation:

    using Microsoft.InformationProtection;
    using Microsoft.Identity.Client;
    
    public class AuthDelegateImplementation : IAuthDelegate
    {
        private ApplicationInfo _appInfo;
        private IPublicClientApplication _app;
    
        public AuthDelegateImplementation(ApplicationInfo appInfo)
        {
            _appInfo = appInfo;
        }
    
        public string AcquireToken(Identity identity, string authority, string resource, string claims)
        {
            var authorityUri = new Uri(authority);
            authority = string.Format("https://{0}/{1}", authorityUri.Host, "<Tenant-GUID>");
    
            _app = PublicClientApplicationBuilder
                .Create(_appInfo.ApplicationId)
                .WithAuthority(authority)
                .WithDefaultRedirectUri()
                .Build();
    
            var accounts = _app.GetAccountsAsync().GetAwaiter().GetResult();
    
            string[] scopes = new string[]
            {
                resource.EndsWith('/') ? $"{resource}.default" : $"{resource}/.default"
            };
    
            var result = _app.AcquireTokenInteractive(scopes)
                .WithAccount(accounts.FirstOrDefault())
                .WithPrompt(Prompt.SelectAccount)
                .ExecuteAsync()
                .ConfigureAwait(false)
                .GetAwaiter()
                .GetResult();
    
            return result.AccessToken;
        }
    }
    
  1. Hinzufügen einer neuen Klasse mit dem Namen ConsentDelegateImplementation:

    using Microsoft.InformationProtection;
    
    public class ConsentDelegateImplementation : IConsentDelegate
    {
        public Consent GetUserConsent(string url)
        {
            return Consent.Accept;
        }
    }
    

Initialisieren des Policy-Profils und des Moduls

  1. Aktualisieren Sie Program.cs, um MipContext zu erstellen, laden Sie ein PolicyProfile, und fügen Sie folgendes PolicyEngine hinzu:

    using Microsoft.InformationProtection;
    using Microsoft.InformationProtection.Policy;
    
    // Application info for Microsoft Entra app registration
    ApplicationInfo appInfo = new ApplicationInfo()
    {
        ApplicationId = "<application-id>",
        ApplicationName = "MIP SDK Policy Quickstart",
        ApplicationVersion = "1.0"
    };
    
    // Create MipConfiguration and MipContext
    var mipConfiguration = new MipConfiguration(appInfo, "mip_data", LogLevel.Trace, false, CacheStorageType.OnDiskEncrypted);
    var mipContext = MIP.CreateMipContext(mipConfiguration);
    
    // Create auth and consent delegates
    var authDelegate = new AuthDelegateImplementation(appInfo);
    var consentDelegate = new ConsentDelegateImplementation();
    
    // Create PolicyProfile
    var profileSettings = new PolicyProfileSettings(mipContext, CacheStorageType.OnDiskEncrypted);
    var profile = MIP.LoadPolicyProfileAsync(profileSettings).GetAwaiter().GetResult();
    
    // Create PolicyEngine
    var engineSettings = new PolicyEngineSettings(
        id: "<user@contoso.com>",
        authDelegate: authDelegate,
        clientData: "",
        locale: "en-US")
    {
        Identity = new Identity("<user@contoso.com>")
    };
    
    var engine = profile.AddEngineAsync(engineSettings).GetAwaiter().GetResult();
    
    Console.WriteLine("Policy engine loaded successfully.");
    
  2. Erstellen und Testen. Die Anwendung sollte initialisieren und eine Verbindung mit dem Richtliniendienst herstellen.

Nächste Schritte