Code Monkey home page Code Monkey logo

Comments (5)

bezysoftware avatar bezysoftware commented on June 12, 2024

Hi, can you be more specific? What do you want to achieve? Do you want to have your own server which would be authenticating users?

from firebase-authentication-dotnet.

LeoVernaza avatar LeoVernaza commented on June 12, 2024

Hi, I have my own server to authenticate the users and return a JWT token.
I'm using the firebase SDK for iOS to authenticate my user using my server JWT token like this:

FIRAuth.auth()?.signInWithCustomToken(userMyServerToken) { (user, error) in

}

it's working on iOS, I need do the same thing with my c# application.

from firebase-authentication-dotnet.

bezysoftware avatar bezysoftware commented on June 12, 2024

you don't need to use this package to use your custom JWT token. You can use the FirebaseDatabase.net package directly with the token you get from your service

from firebase-authentication-dotnet.

bernhardgessler avatar bernhardgessler commented on June 12, 2024

I couldn't get it to work using the (v3) custom token directly. And according to documentation it shouldn't. The custom tokens are different from firebase tokens and the admin SDK's method for verifying tokens does not work on custom tokens. The client needs to exchange it for a firebase token first.

Observing the JS client library, I figured out a possible implementation:

private const string GoogleCustomAuthUrl = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key={0}";
public async Task<FirebaseAuthLink> SignInWithCustomTokenAsync(string customToken)
        {
            string content = string.Format("{{\"token\":\"{0}\",\"returnSecureToken\":true}}", new object[1]
            {
                (object) customToken
            });
            FirebaseAuthLink firebaseAuthLink = await this.ExecuteWithPostContentAsync(GoogleCustomAuthUrl, content).ConfigureAwait(false);
            return firebaseAuthLink;
        }

Unfortunately the user object stays empty. So I quickly put together a second method, again immitating the js client's behaviour:

private User GetUser(string idToken)
        {
            var request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo?key=" + this.authConfig.ApiKey);
            request.ContentType = "application/json";
            request.Method = "POST";

            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                string json = new JavaScriptSerializer().Serialize(new
                {
                    idToken = idToken
                });

                streamWriter.Write(json);
            }

            var response = (HttpWebResponse)request.GetResponse();
            using (var streamReader = new StreamReader(response.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                JObject resultJson = JObject.Parse(result);
                IEnumerable<JToken> usersJson = resultJson["users"];
                var user = JsonConvert.DeserializeObject<User>(usersJson.First().ToString());
                return user;
            }
        }

and added a line into the first method:
firebaseAuthLink.User = GetUser(firebaseAuthLink.FirebaseToken);

Any thoughts or experiences? Any chance to make that part of your (awesome) lib?

from firebase-authentication-dotnet.

bezysoftware avatar bezysoftware commented on June 12, 2024

Feel free to make a PR, I will review it and accept it ;)

from firebase-authentication-dotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.