簡體   English   中英

將 Appwrite auth 連接到 ASP.NET Core 7

[英]Connect Appwrite auth with ASP.NET Core 7

我正在嘗試使用我的 ASP.NET Core 7 Web API 對 Appwrite 用戶進行身份驗證。過去,我使用 Firebase 來實現 function,如下所示:

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"]
                    };
                }
            );
}

這驗證了針對 firebase API 的請求,但我看不出如何為 Appwrite 實現類似的功能。 文檔也沒有提到任何有用的東西。

有誰知道如何實現這一目標?

不幸的是,Appwrite 還沒有 .NET SDK,因此您必須手動撥打 API。 我不太了解 .NET,但我使用API 規范和 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);
}

如果成功,您可以獲取X-Fallback-Cookies響應 header 並將其用於未來的請求。

否則,如果您不想創建 session 服務器端並且您有從前端生成的 Appwrite JWT 令牌,您可以對 Appwrite 進行 API 調用並在X-Appwrite-JWT header 中傳遞 JWT 令牌以發出請求代表用戶。

有關直接使用 Appwrite REST API 的更多信息,請參閱REST 文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM