快速入门:客户端应用程序初始化 - 策略 SDK (C#)

本快速入门指南介绍如何在运行时实施用于策略 SDK 的 MIP SDK .NET 包装器中的客户端初始化模式。

注释

使用 MIP .NET策略 SDK 的任何客户端应用程序都需要本快速入门中的步骤。 在开始之前完成 设置和配置 MIP SDK 。

先决条件

如果尚未这样做,请确保:

创建 Visual Studio 解决方案和项目

  1. 打开 Visual Studio 2022 或更高版本,选择 “文件 ”菜单“ 新建”和 “项目”。

    • 选择 控制台应用 (面向 .NET 8 或更高版本)。
    • 提供项目 的名称 和 位置 。
  2. 为 MIP 策略 SDK 和 MSAL 添加 NuGet 包:

    • 在解决方案资源管理器中,右键单击项目节点并选择“管理 NuGet 包...”。
    • 选择 “浏览”,在搜索框中输入“ Microsoft.InformationProtection”,并安装 Microsoft.InformationProtection.Policy 包。
    • 搜索并安装 Microsoft.Identity.Client。

实施身份验证委托

  1. 添加名为 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. 添加名为 ConsentDelegateImplementation: 的新类:

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

初始化策略配置文件和引擎

  1. 更新Program.cs以创建MipContext、加载PolicyProfile和添加:PolicyEngine

    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. 构建和测试。 应用程序应初始化并连接到策略服务。

后续步骤