0

I am trying to authenticate an Appwrite user with my ASP.NET Core 7 Web API. In the past, I used Firebase for this with which I was able to implement the function as following:

private static void ConfigureFirebaseAuthentication(IServiceCollection services,
                                                    IConfiguration configuration)
{
    var options = new AppOptions() { Credential = GoogleCredential.FromFile("firebase-config.json") };

    FirebaseApp.Create(options);

    services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(
                opt =>
                {
                    opt.IncludeErrorDetails = true;
                    opt.Authority = configuration["FirebaseAuthentication:ValidIssuer"];
                    opt.TokenValidationParameters = new()
                    {
                        ValidateIssuer = true,
                        ValidateAudience = true,
                        ValidateLifetime = true,
                        ValidateIssuerSigningKey = true,
                        ValidIssuer = configuration["FirebaseAuthentication:ValidIssuer"],
                        ValidAudience = configuration["FirebaseAuthentication:ValidAudience"]
                    };
                }
            );
}

This validated the request against the firebase API, but I don't see how I am able to implement something similar for Appwrite. Also the docs don't mention anything helpful.

Does anyone know how to achieve this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
DirtyNative
  • 2,553
  • 2
  • 33
  • 58

1 Answers1

0

Unfortunately, Appwrite doesn't have a .NET SDK yet so you would have to manually make the API call. I don't know .NET very well, but I generated code using the API specs and Insomnia:

var client = new HttpClient();
var request = new HttpRequestMessage
{
    Method = HttpMethod.Post,
    RequestUri = new Uri("https://[HOSTNAME]/v1/account/sessions/email"),
    Headers =
    {
        { "X-Appwrite-Project", "[PROJECT ID]" },
    },
    Content = new StringContent("{\n  \"email\": \"[EMAIL]\",\n  \"password\": \"[PASSWORD]\"\n}")
    {
        Headers =
        {
            ContentType = new MediaTypeHeaderValue("application/json")
        }
    }
};
using (var response = await client.SendAsync(request))
{
    response.EnsureSuccessStatusCode();
    var body = await response.Content.ReadAsStringAsync();
    Console.WriteLine(body);
}

If this is successful, you can grab the X-Fallback-Cookies response header and use that for future requests.

Otherwise, if you don't want to create a session server side and you have an Appwrite JWT token generated from your front end, you can make API calls to Appwrite and pass the JWT token in the X-Appwrite-JWT header to make requests on behalf of the user.

For more information on working directly with the Appwrite REST API, refer to the REST docs.

Steven Nguyen
  • 452
  • 4
  • 4