从ASP.NET Core API向Firebase身份验证添加用户声明 [英] Add user claims to firebase auth from asp.net core api

查看:74
本文介绍了从ASP.NET Core API向Firebase身份验证添加用户声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个asp.net核心api和仅使用[Authorize]属性的有效Firebase身份验证,如何将自定义声明添加到令牌中以使用[Authorize(Policy ="admin")]?管理SDK仅适用于node.js,Java,python或go,我找不到有关如何直接使用Firebase Admin API的任何文档.据我了解,用户声明必须存储在Firebase Auth后端中.

Given an asp.net core api and working firebase auth using only the [Authorize] attribute, how can I add custom claims to the token to use [Authorize(Policy = "admin")]? The admin SDK is only available for node.js, Java, python or go and I can't find any docs on how to use the Firebase Admin API directly. From my understanding the user claims have to be stored in the Firebase Auth backend.

推荐答案

自从发布Firebase Admin .NET SDK 1.1以来,支持使用自定义声明进行控制访问"功能.

Since the Firebase Admin .NET SDK 1.1 is released the Control Access With Custom Claims feature is supported.

您可以在此处找到存储库: https://github.com/firebase/firebase- admin-dotnet

You can find the repository here: https://github.com/firebase/firebase-admin-dotnet

设置指南和文档: https://firebase.google.com/docs/admin/设置

解决方案:

// Initialize the admin SDK with your service account keys
FirebaseApp.Create(new AppOptions()
{
    Credential = GoogleCredential.FromFile("path/to/serviceAccountKey.json"),
});

// Create the custom user claim that has the role key
var claims = new Dictionary<string, object>
{
    { ClaimTypes.Role, "Administrator" }
};

// This will call the Firebase Auth server and set the custom claim for the user
await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(uid, claims);

这篇关于从ASP.NET Core API向Firebase身份验证添加用户声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆