[英]Firebase Authenticate Simple Login
TL; DR:有什么方法可以將auth=CREDENTIALS
與Firebase中的簡單登錄名(電子郵件/密碼)一起使用?
我正在嘗試將C#應用程序的用戶連接到Firebase。 我可以使用我的Secret Token設置幾乎所有呼叫,但是現在至少需要獲得當前用戶的UID,這樣我才能知道數據應該發送到哪里。
使用我的秘密令牌作為登錄名時,我進行PUSH,PUT,GET請求的方式是這樣的:
var authToken = "SECRET";
url = "https://MyLocation.firebaseio.com/" + url + ".json?auth=" + authToken;
return WebRequest.Create(url);
但是,現在我想獲得一些支持電子郵件/密碼簡單登錄的功能,例如:
var authToken = "{email:an@email.com, password:thePassword}";
url = "https://MyLocation.firebaseio.com/" + url + ".json?auth=" + authToken;
return WebRequest.Create(url);
我使用CURL的嘗試未成功……也許沒有辦法嗎? 或任何建議?
謝謝您的幫助!
我與Firebase的支持人員進行了交談,找到了一個臨時解決方案和一個真正的解決方案。
真正的解決方案:使用Firebase作為“數據庫”,在所有環境中手動管理用戶及其密碼。 這基本上就是我要解決的問題。 使用Firebase自定義auth即可解決。
臨時解決方案:(而且我所做的我不需要真正的解決方案提供的安全性)
Base64標識符:
byte[] result = System.Text.Encoding.UTF8.GetBytes(email); email = Convert.ToBase64String(result);
通過REST將所需的信息放入,推送並打補丁到firebaseio.com/Base64
在使用JavaScript的用戶界面中,執行相同的過程以使用base64.min.js之類的方法在用戶處讀取/寫入數據。
var ref = new Firebase("https://aFirebase.firebaseio.com");
//Things happen
...
//We register a user
function createUser(email, password){
//Allows us to create a user within firebase
ref.createUser({
email : email,
password : password
}, function(error, userData){
if (error) {
//The creation of the user failed
alert(error);
} else {
//The creation of the user succeeded
console.log("Successfully created user account with uid:", userData.uid);
//We make sure we are at the correct position in our firebase
ref = ref.root().child(base64.encode(email));
//We check if the child exist
if(ref == ref.root()){
//The child doesn't exist
//We have to create it
user = {};
//Set the child with a value for the UID, that will fit with the rules
user[base64.encode(email)] = {uid:userData.uid};
//We set the new child with his value in firebase
ref.set(user);
}else{
//The child exist, we can update his information to go accordingly with our rules
ref.update({uid:userData.uid});
}
//Who wants to register and then not be logged in?
//We can add something upon login if his email is not validated...
login(email, password);
}
}
);
}
現在,我們必須更新Firebase中的規則:
{ "rules": { "$uid":{ ".read":"!(data.child('uid').exists() == true) || data.child('uid').val() == auth.uid", ".write":"!(data.child('uid').exists() == true) || data.child('uid').val() == auth.uid" } } }
這樣,該應用程序就可以安全地運行(只要用戶使用將在其中設置規則的C#應用程序和JS應用程序)。
對於WebApi應用程序,可以將JWT令牌與OWIN管道一起使用。
app.UseJwtBearerAuthentication(new JwtBearerAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AllowedAudiences = new[] { FirebaseValidAudience },
Provider = new OAuthBearerAuthenticationProvider
{
OnValidateIdentity = OnValidateIdentity
},
TokenValidationParameters = new TokenValidationParameters
{
IssuerSigningKeys = issuerSigningKeys,
ValidAudience = FirebaseValidAudience,
ValidIssuer = FirebaseValidIssuer,
IssuerSigningKeyResolver = (arbitrarily, declaring, these, parameters) => issuerSigningKeys
}
});
這是Firebase ASP.NET WebApi身份驗證應用程序的示例: https : //github.com/PavelDumin/firebase-webapi-auth
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.