我試圖使用自定義令牌實現Firebase 3身份驗證機制(如https:// firebase.google.com/docs/auth/server/create中所述-custom的令牌)。Firebase 3:使用.net和c創建自定義身份驗證令牌#
我的服務器是ASP.NET MVC應用程序。
因此,根據說明(https://firebase.google.com/docs/server/setup),我爲我的Firebase應用程序創建了一個服務帳戶,並生成了一個'.p12'格式的密鑰。
之後,根據此處的說明(https://firebase.google.com/docs/auth/server/create-custom-tokens#create_custom_tokens_using_a_third-party_jwt_library),我嘗試生成自定義令牌並使用上一步收到的密鑰對其進行簽名。對於令牌生成我使用微軟SystemIdentityModel.Tokens.Jwt庫,所以代碼如下所示:
var now = DateTime.UtcNow;
var tokenHandler = new JwtSecurityTokenHandler();
var key = new X509AsymmetricSecurityKey(new X509Certificate2(p12path, p12pwd));
var signinCredentials = new SigningCredentials(key, "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", "http://www.w3.org/2001/04/xmlenc#rsa-sha256");
Int32 nowInUnixTimestamp = (Int32)(now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
var token = tokenHandler.CreateToken(
issuer: serviceAccountEmail,
audience: "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
signingCredentials: signinCredentials,
subject: new ClaimsIdentity(new Claim[]
{
new Claim("sub", serviceAccountEmail),
new Claim("iat", nowInUnixTimestamp.ToString()),
new Claim("exp", (nowInUnixTimestamp + (60*60)).ToString()),
new Claim("uid", uid)
})
);
var tokenString = tokenHandler.WriteToken(token);
然後試圖使用作出反應的Javascript火力地堡SDK機應用程序,用下面的代碼在用戶登錄:
//omitting initialization code
firebase.auth().signInWithCustomToken(firebaseJWT).catch(function(error) {
console.log('Error authenticating Firebase user. Code: ' + error.code + ' Message: ' + error.message);
});
但得到了一個錯誤,從火力地堡說:
錯誤認證火力地堡用戶。代碼:auth/invalid-custom-token消息:自定義令牌格式不正確。請檢查文檔。
嘗試爲令牌過期控制添加不同的聲明也沒有幫助。
另外我試圖用「dvsekhvalnov/jose-jwt」庫生成標記,但無法使用「RS256」算法得到它。
所以問題:
什麼我做錯了什麼建議嗎?
我認識到,通過該鏈接http://stackoverflow.com/questions/37408684/is-it-still-possible-to-do-server-side-verification-of-tokens-描述的令牌格式in-firebase-3/37492640#37492640是由Firebase本身發佈的令牌,因此第一個問題不再是問題。 –
以下是Google支持同一問題的答案:**「我看到了您的帖子,因此您已經有了一個解決方法。對於令牌格式,您應該始終遵循最新文檔中的內容。關於現在的身份驗證,我們正在盡全力讓事情繼續進行下去。 請留意我們的發佈說明中的任何進一步更新,如有需要請隨時與我們聯繫。「**所以看起來像解決方法是現在最好的選擇。 –