Firebase 验证简单登录
Firebase Authenticate Simple Login
TL;DR : 有什么方法可以在 Firebase 中使用 auth=CREDENTIALS
和简单登录 (Email/Password) 吗?
我正在尝试将我的 C# 应用程序的用户连接到我的 Firebase。我可以使用我的秘密令牌设置几乎所有调用,但现在我至少需要能够获取当前用户 UID,以便我知道应该将数据发送到哪里。
我处理 PUSH、PUT、GET 请求的方式是这样的,使用我的秘密令牌作为登录:
var authToken = "SECRET";
url = "https://MyLocation.firebaseio.com/" + url + ".json?auth=" + authToken;
return WebRequest.Create(url);
但现在我想要一些支持 Email/Password 简单登录的东西,像这样:
var authToken = "{email:an@email.com, password:thePassword}";
url = "https://MyLocation.firebaseio.com/" + url + ".json?auth=" + authToken;
return WebRequest.Create(url);
我使用 CURL 的尝试没有成功...也许没有办法做到这一点?或者有什么建议吗?
感谢您的帮助!
我与 Firebase 的支持人员进行了交谈,找到了一个临时解决方案,一个真正的解决方案。
真正的解决方案:在所有环境中手动管理用户及其密码,使用 Firebase 作为 "Database"。这基本上就是我试图用我的问题做的。决定使用 Firebase custom auth.
临时解决方案:(我所做的并不需要像真正的解决方案那样安全)
- 获取可识别当前用户的信息。在这里,我什至不用问他就可以得到当前用户的电子邮件。
Base64标识符:
byte[] result = System.Text.Encoding.UTF8.GetBytes(email);
email = Convert.ToBase64String(result);
通过 REST 将所需信息放入、推送、修补到 firebaseio.com/Base64
在使用 JavaScript 的用户界面中,对用户处的 read/write 数据执行相同的处理,使用类似 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
TL;DR : 有什么方法可以在 Firebase 中使用 auth=CREDENTIALS
和简单登录 (Email/Password) 吗?
我正在尝试将我的 C# 应用程序的用户连接到我的 Firebase。我可以使用我的秘密令牌设置几乎所有调用,但现在我至少需要能够获取当前用户 UID,以便我知道应该将数据发送到哪里。
我处理 PUSH、PUT、GET 请求的方式是这样的,使用我的秘密令牌作为登录:
var authToken = "SECRET";
url = "https://MyLocation.firebaseio.com/" + url + ".json?auth=" + authToken;
return WebRequest.Create(url);
但现在我想要一些支持 Email/Password 简单登录的东西,像这样:
var authToken = "{email:an@email.com, password:thePassword}";
url = "https://MyLocation.firebaseio.com/" + url + ".json?auth=" + authToken;
return WebRequest.Create(url);
我使用 CURL 的尝试没有成功...也许没有办法做到这一点?或者有什么建议吗?
感谢您的帮助!
我与 Firebase 的支持人员进行了交谈,找到了一个临时解决方案,一个真正的解决方案。
真正的解决方案:在所有环境中手动管理用户及其密码,使用 Firebase 作为 "Database"。这基本上就是我试图用我的问题做的。决定使用 Firebase custom auth.
临时解决方案:(我所做的并不需要像真正的解决方案那样安全)
- 获取可识别当前用户的信息。在这里,我什至不用问他就可以得到当前用户的电子邮件。
Base64标识符:
byte[] result = System.Text.Encoding.UTF8.GetBytes(email); email = Convert.ToBase64String(result);
通过 REST 将所需信息放入、推送、修补到
firebaseio.com/Base64
在使用 JavaScript 的用户界面中,对用户处的 read/write 数据执行相同的处理,使用类似 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