> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 管理机密(预览版)

> 本文档提供机密管理功能的相关信息,该功能使客户能够安全地将机密部署到游戏服务器中。

借助此新功能,游戏开发人员可以安全地上传、列出、更新和使用游戏服务器上的机密,例如检测密钥和密钥。

## 如何使用机密管理功能

要有效地管理和使用机密,推荐的工作流程包括:

1. 添加机密(可以对多个机密重复执行此步骤)
2. 在创建新构建时按名称引用机密
3. 从游戏服务器访问机密
4. 根据需要更新机密

### 添加机密

可以通过 [UploadSecret](https://learn.microsoft.com/en-us/rest/api/playfab/multiplayer/multiplayer-server/upload-secret) API 添加机密。
此 API 允许将新机密上传到服务,或通过将 **ForceUpdate** 参数设置为 **true** 来更新现有机密。

### 部署机密

要在新创建的 VM 中部署机密,请在 CreateBuild API 中使用 GameSecretReferences 参数将机密包含在构建中(适用于基于容器和基于进程的部署)。

<Note>
  创建构建后,无法向其添加新机密;但是,更新现有机密将允许在新 VM 中使用其更新后的值(现有 VM 将保留原始值)。
</Note>

以下是引用机密的示例:

```csharp theme={null}
var request = new CreateBuildWithCustomContainerRequest()
{
    ContainerImageReference = new ContainerImageReference()
    {
        ImageName = "testimagename",
        Tag = "0.1"
    },
    ContainerFlavor = ContainerFlavor.CustomLinux,
    BuildName = "testbuildwithvmstartupscript",
    VmSize = AzureVmSize.Standard_D2as_v4,
    MultiplayerServerCountPerVm = 3,
    Ports = new List<Port>
    {
        new Port()
        {
            Name = "port",
            Num = 123,
            Protocol = ProtocolType.TCP
        }
    },
    RegionConfigurations = new List<BuildRegionParams>
    {
        new BuildRegionParams()
        {
            Region = "EastUs",
            StandbyServers = 3,
            MaxServers = 6
        }
    },
    GameSecretReferences = new[]
    {
        new GameSecretReferenceParams
        {
            Name = "SecretName"
        }
    }
};
var result = await PlayFabMultiplayerAPI.CreateBuildWithCustomContainerAsync(request);
```

### 访问机密

在新构建中添加并引用机密后,游戏服务器可以通过每个游戏服务器内的环境变量访问这些机密值,无论是基于容器还是基于进程的服务器。

环境变量的命名约定为:

```shell theme={null}
PF_MPS_SECRET_<Secret Name>
```

### 删除机密

要删除旧机密,请使用 [DeleteSecret](https://learn.microsoft.com/en-us/rest/api/playfab/multiplayer/multiplayer-server/delete-secret) API。

在尝试删除机密之前,请确保任何构建中都未使用该机密,否则删除将不会成功。

## 在 VM 启动脚本中使用机密

VM 启动脚本可以通过引用如上所述的环境变量来使用机密值。例如,这使得在启动任何游戏服务器之前使用遥测密钥或其他必要值成为可能。

## 另请参阅

* [使用 PowerShell/API 部署构建](/services/playfab/multiplayer/servers/deploy-using-powershell-api)
* [在 VM 创建期间运行自定义脚本 - VmStartupScript(预览版)](/services/playfab/multiplayer/servers/vmstartupscript)


## Related topics

- [在预览版期间使用 Playtest](/zh-CN/publishing/game-publishing/concepts/playtest-preview.md)
- [事件分区快速入门（预览）](/zh-CN/services/playfab/data-analytics/export-data/event-partitioning-quickstart.md)
- [FMA:XR-117 Beta/游戏预览通知](/zh-CN/publishing/certification/fma/xr-117.md)
- [使用玩家自定义属性进行高级分段](/zh-CN/services/playfab/live-service-management/game-configuration/segmentation/advanced-segmentation.md)
- [Playtest 概述](/zh-CN/publishing/game-publishing/concepts/playtest.md)
