---
sidebar_position: 5
slug: /dev/relay
---

# 98.5、中继验证

:::tip[说明]
验证通过则允许中继
```
//初始化
LinkerMessengerEntry.Initialize();
//在这里注入
LinkerMessengerEntry.AddService<MyRelayServerValidator>();

//构建
LinkerMessengerEntry.Build();

RelayServerValidatorTransfer relayServerValidatorTransfer 
= LinkerMessengerEntry.GetService<RelayServerValidatorTransfer>();
//可以删除默认的一些验证,secretKey中继密钥
//relayServerValidatorTransfer.RemoveValidators(new List<string>{ "secretKey"});

ICommonStore commonStore = LinkerMessengerEntry.GetService<ICommonStore>();
//服务端则添加
if((commonStore.Modes & CommonModes.Server) == CommonModes.Server)
{
    relayServerValidatorTransfer.AddValidators(new List<IRelayServerValidator> { 
        LinkerMessengerEntry.GetService<MyRelayServerValidator>() 
    });
}

//运行
LinkerMessengerEntry.Setup();


public sealed class MyRelayServerValidator : IRelayServerValidator
{
    public string Name => "myRelayServerValidator";
   
    /// <summary>
    /// 验证
    /// </summary>
    /// <param name="relayInfo">中继信息</param>
    /// <param name="fromMachine">来源客户端</param>
    /// <param name="toMachine">目标客户端，可能为null</param>
    /// <returns>返回不为空则为错误信息，中继失败</returns>
    public async Task<string> Validate(linker.messenger.relay.client.transport.RelayInfo relayInfo,
     SignCacheInfo fromMachine, SignCacheInfo toMachine)
    {
        await Task.CompletedTask;
        return string.Empty;
    }
}
```
:::