C# 如何通过FirebaseAdmin SDK从自定义令牌获取ID令牌?

C# 如何通过FirebaseAdmin SDK从自定义令牌获取ID令牌?,c#,.net-core,firebase-authentication,C#,.net Core,Firebase Authentication,如何从自定义令牌获取ID令牌 [Fact] public void Get_ID_Token_For_Service_Account_Test() { using (Stream stream = new FileStream(ServiceAccountJsonKeyFilePath, FileMode.Open, FileAccess.Read)) { ServiceAccountCredential credential = ServiceAccountCr

如何从自定义令牌获取ID令牌

[Fact]
public void Get_ID_Token_For_Service_Account_Test()
{
    using (Stream stream = new FileStream(ServiceAccountJsonKeyFilePath, FileMode.Open, FileAccess.Read))
    {
        ServiceAccountCredential credential = ServiceAccountCredential.FromServiceAccountData(stream);
        FirebaseApp.Create(new AppOptions
        {
            Credential = GoogleCredential.FromServiceAccountCredential(credential),
            ServiceAccountId = ServiceAccountId,
        });
        var uid = "Some UID";
        var additionalClaims = new Dictionary<string, object>
        {
            {"dmitry", "pavlov"}
        };
        string customToken = FirebaseAuth.DefaultInstance.CreateCustomTokenAsync(uid, additionalClaims).Result;

        string idToken= null; // How to get this? 

        FirebaseToken token = FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken, CancellationToken.None).Result;

        Assert.NotNull(token);
        Assert.True(token.Claims.ContainsKey("dmitry"));
    }
}
[事实]
public void获取\u ID\u令牌\u用于\u服务\u帐户\u测试()
{
使用(Stream Stream=newfilestream(ServiceAccountJsonKeyFilePath,FileMode.Open,FileAccess.Read))
{
ServiceAccountCredential credential=ServiceAccountCredential.FromServiceAccountData(流);
FirebaseApp.Create(新建AppOptions
{
凭证=谷歌凭证。来自ServiceAccountCredential(凭证),
ServiceAccountId=ServiceAccountId,
});
var uid=“Some uid”;
var additionalClaims=新字典
{
{“德米特里”,“巴甫洛夫”}
};
字符串customToken=FirebaseAuth.DefaultInstance.CreateCustomTokenAsync(uid,AdditionalClaimes).Result;
string idToken=null;//如何获取此值?
FirebaseToken token=FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken,CancellationToken.None);
Assert.NotNull(令牌);
Assert.True(token.Claims.ContainsKey(“dmitry”);
}
}

我看到了一些其他语言/平台的示例,但没有看到C#-如何通过此处的当前用户获取ID令牌-。但对于C#,UserRecord和FirebaseAuth都不提供ID令牌。非常感谢任何指点

我在
FirebaseAdmin
集成测试中找到了获取ID令牌的方法-请参阅方法。我唯一需要调整的是基本URL:根据文档,它应该是

https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken

API键
指的是
Web API键
,可在管理控制台的页面上获得

因此,调整后的代码如下所示:

private static async Task<string> SignInWithCustomTokenAsync(string customToken)
{
    string apiKey = "..."; // see above where to get it. 
    var rb = new Google.Apis.Requests.RequestBuilder
    {
        Method = Google.Apis.Http.HttpConsts.Post,
        BaseUri = new Uri($"https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken")
    };
    rb.AddParameter(RequestParameterType.Query, "key", apiKey);
    var request = rb.CreateRequest();
    var jsonSerializer = Google.Apis.Json.NewtonsoftJsonSerializer.Instance;
    var payload = jsonSerializer.Serialize(new SignInRequest
    {
        CustomToken = customToken,
        ReturnSecureToken = true,
    });
    request.Content = new StringContent(payload, Encoding.UTF8, "application/json");
    using (var client = new HttpClient())
    {
        var response = await client.SendAsync(request);
        response.EnsureSuccessStatusCode();
        var json = await response.Content.ReadAsStringAsync();
        var parsed = jsonSerializer.Deserialize<SignInResponse>(json);
        return parsed.IdToken;
    }
}
private static async Task SignInWithCustomTokenAsync(字符串customToken)
{
字符串apiKey=“…”;//请参见上面的获取位置。
var rb=new Google.api.Requests.RequestBuilder
{
方法=Google.api.Http.HttpConsts.Post,
BaseUri=新Uri($”https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken")
};
AddParameter(RequestParameterType.Query,“key”,apiKey);
var request=rb.CreateRequest();
var jsonSerializer=Google.api.Json.NewtonsoftJsonSerializer.Instance;
var payload=jsonSerializer.Serialize(新的SignInRequest
{
CustomToken=CustomToken,
ReturnSecureToken=true,
});
request.Content=newstringcontent(有效负载,Encoding.UTF8,“application/json”);
使用(var client=new HttpClient())
{
var response=wait client.sendaync(请求);
response.EnsureSuccessStatusCode();
var json=await response.Content.ReadAsStringAsync();
var parsed=jsonSerializer.Deserialize(json);
返回parsed.IdToken;
}
}

我也提出了这个问题