1

TL;DR:有没有办法auth=CREDENTIALS在 Firebase 中使用简单登录(电子邮件/密码)?

我正在尝试将我的 C# 应用程序的用户连接到我的 Firebase。我可以使用我的秘密令牌设置几乎所有的调用,但现在我至少需要能够获取当前用户 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 的尝试没有成功......也许没有办法做到这一点?或任何建议?

谢谢您的帮助!

4

2 回答 2

1

我与 Firebase 的支持人员进行了交谈,找到了一个临时解决方案和一个真正的解决方案。

真正的解决方案:在所有环境中手动管理用户及其密码,使用 Firebase 作为“数据库”。这基本上就是我试图解决我的问题的原因。使用Firebase 自定义身份验证的决心。

临时解决方案:(我所做的因为我不需要真正解决方案提供的安全性)

  1. 获取可以识别当前用户的东西。在这里,我什至不问他就可以获得当前用户的电子邮件。
  2. Base64 标识符:

    byte[] result = System.Text.Encoding.UTF8.GetBytes(email);
    email = Convert.ToBase64String(result);
    
  3. 通过 REST 将所需信息放入、推送、修补到firebaseio.com/Base64

  4. 在使用 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);
                }
            }
        );
    }
  1. 现在我们必须更新 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 应用程序)。

于 2015-03-25T19:51:44.437 回答
0

对于 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

于 2019-08-31T14:33:44.417 回答