简体   繁体   中英

Use c# application and firebase authentication to log in

I have set up a real time firebase database that will be used to store some information for a University project. So only trivial data will be stored with no sensitive information. I would like to create a c# application that can login using the built in firebase authentication.

I have successfully connected my c# and firebase together using the following code

IFirebaseConfig ifc = new FirebaseConfig()
    {
        AuthSecret = "secretcode",
        BasePath = "https://mydbpath/"
    };

    IFirebaseClient client; 

And i now wish to be able to add new users to the firebase as well as let them log in. I noticed IFirebaseClient has the

CreateUser(string email , String passwword) 

but i am unsure on how to use them since when i simply tried adding a user on a button click I get the following error.

FireSharp.Exceptions.FirebaseException: Request responded with status 
code=MethodNotAllowed, response=<html>
<head><title>405 Not Allowed</title></head>
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx</center>
</body>
</html>

I am unsure whether this has something to do with my settings in the firebase or with my implementation of the ciode so any links to documentation or assistance would be much appreciated.

In your Firebase realtime database make sure you have configured rules correctly in order to read or change data something like this:

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    }
  }
}

Further information: https://firebase.google.com/docs/database/security

to use Firebase Authentication you can use the Admin Auth Api and you can use it in your project after you add the SDK Setup Instructions

this is a sample code to add a new user:

UserRecordArgs args = new UserRecordArgs()
{
    Email = "user@example.com",
    EmailVerified = false,
    PhoneNumber = "+11234567890",
    Password = "secretPassword",
    DisplayName = "John Doe",
    PhotoUrl = "http://www.example.com/12345678/photo.png",
    Disabled = false,
};
UserRecord userRecord = await FirebaseAuth.DefaultInstance.CreateUserAsync(args);
// See the UserRecord reference doc for the contents of userRecord.
Console.WriteLine($"Successfully created new user: {userRecord.Uid}");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM