Skip to content

微软验证器 ​

用于拥有已购买游戏的玩家或游玩开启正版验证的服务器

命名空间 StarLight_Core.Authentication

构造函数 ​

csharp
public MicrosoftAuthentication(string clientId)
{
    ClientId = clientId;
}
参数类型描述
clientIdstring在 Azure 中注册的 ClientId

IMPORTANT

自2023年6月15日起, 新申请的 ClientId 必须向 Mojang 提交申请才能访问 Minecraft 账户 详细信息

方法参考 ​

RetrieveDeviceCodeInfo 获取设备代码 ​

csharp
public async ValueTask<RetrieveDeviceCode> RetrieveDeviceCodeInfo()
返回值描述
RetrieveDeviceCode参数详解

GetTokenResponse 轮询获取 Token ​

csharp
public async ValueTask<GetTokenResponse> GetTokenResponse(RetrieveDeviceCode deviceCodeInfo)
参数类型描述
deviceCodeInfoRetrieveDeviceCode参数详解
返回值描述
GetTokenResponse参数详解

MicrosoftAuthAsync 异步验证方法 ​

csharp
public async ValueTask<MicrosoftAccount> MicrosoftAuthAsync(GetTokenResponse tokenInfo, Action<string> action, string? refreshToken = null)
参数类型描述
tokenInfoGetTokenResponse参数详解
actionAction<string>进度报告
refreshToken = nullstring?刷新令牌
返回值描述
MicrosoftAccount微软账户类

参数详解 ​

详细 RetrieveDeviceCode 定义 ​

参数类型描述
DeviceCodestring设备代码
UserCodestring用户代码
ClientIdstring在 Azure 中获取的 ClientId
VerificationUristring验证地址
Messagestring提示信息

详细 GetTokenResponse 定义 ​

参数类型描述
AccessTokenstring资源令牌
RefreshTokenstring刷新令牌
ClientIdstring在 Azure 中获取的 ClientId
ExpiresInint过期时间

控制台示例 ​

TIP

控制台示例只是为了更加方便的了解如何使用, 不建议直接复制

csharp
var auth = new MicrosoftAuthentication(clientId);
var deviceCodeInfo = await auth.RetrieveDeviceCodeInfo();
Console.WriteLine(deviceCodeInfo.UserCode + " " + deviceCodeInfo.VerificationUri);
// 打开 deviceCodeInfo.VerificationUri 引导用户在其间输入 deviceCodeInfo.UserCode 以便完成登录
var tokenInfo = await auth.GetTokenResponse(deviceCodeInfo);
var userInfo = await auth.MicrosoftAuthAsync(tokenInfo, x =>
{
    Console.WriteLine(x); // 当前进度输出
});

所有内容均以 CC BY-NC-SA 4.0 许可协议进行共享