Hızlı Başlangıç Rehberi: İstemci uygulaması ilk yükleme - Politika SDK'sı (C#)

Bu hızlı başlangıçta, çalışma zamanında İlke SDK'sı için MIP SDK .NET sarmalayıcısı tarafından kullanılan istemci başlatma deseninin nasıl uygulanacağını gösteriyoruz.

Uyarı

Bu hızlı başlangıçta özetlenen adımlar, MIP .NET İlkesi SDK'sını kullanan tüm istemci uygulamaları için gereklidir. Bu hızlı başlangıç , MIP SDK'sı ayarlandıktan ve yapılandırıldıktan sonra tamamlanmalıdır.

Önkoşullar

Henüz yapmadıysanız şunları yaptığınızdan emin olun:

Visual Studio çözümü ve projesi oluşturma

  1. Visual Studio 2022 veya sonraki bir sürümünü açın, Dosya menüsünü ( Yeni, Proje) seçin.

    • Konsol Uygulaması (.NET 8 veya üzerini hedefleme) seçeneğini belirleyin.
    • Proje için bir Ad ve Konum belirtin.
  2. MIP İlkesi SDK'sı ve MSAL için NuGet paketlerini ekleyin:

    • Çözüm Gezgini'nde proje düğümüne sağ tıklayın ve NuGet paketlerini yönet...'i seçin.
    • Gözat'ı seçin, arama kutusuna "Microsoft.InformationProtection" yazın ve Microsoft.InformationProtection.Policy paketini yükleyin.
    • Microsoft.Identity.Client'ı arayın ve yükleyin.

Kimlik doğrulama delegesi oluşturma

  1. adlı AuthDelegateImplementationyeni bir sınıf ekleyin:

    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. adlı ConsentDelegateImplementationyeni bir sınıf ekleyin:

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

İlke profilini ve motorunu başlatma

  1. Program.cs güncelleştirin ve MipContext öğesini oluşturun, bir PolicyProfile yükleyin ve bir PolicyEngine ekleyin:

    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);
    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, consentDelegate);
    var profile = MIP.LoadPolicyProfileAsync(profileSettings).GetAwaiter().GetResult();
    
    // Create PolicyEngine
    var engineSettings = new PolicyEngineSettings(
        identity: new Identity("<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. Oluştur ve test et. Uygulamanın başlatılması ve İlke hizmetine bağlanması gerekir.

Sonraki Adımlar