Azure函数应用社交登录认证授权方案合理性及优化咨询
Azure JSON-RPC函数应用认证授权方案咨询
需求概述
我在Azure上部署了一个托管JSON-RPC端点的简易函数应用,供原生CLI应用调用,需构建满足以下要求的认证与授权体系:
- 支持Discord社交登录
- 细粒度授权:根据JSON-RPC调用的方法控制用户权限
- 函数代码简洁,认证与授权最好在栈的早期阶段处理
我熟悉函数应用的基础设施搭建及CI/CD流程,但对Azure认证/授权与APIM、应用注册、OAuth、OIDC、Azure B2C及*Identity Experience Framework(IEF)*的协同机制缺乏清晰认知,暂未找到使用Azure原生产品实现该需求的直接路径。
当前测试环境与配置
当前采用Azure API Management(APIM)及已注册应用的B2C租户:
- APIM配置:已将函数应用注册为API,计划通过入站
validate-jwt策略实现认证,同时使用以下入站处理规则实现授权:
<!-- Step 2: Extract JSON-RPC Method from Body --> <set-variable name="jsonRpcBody" value="@(context.Request.Body.As<string>(preserveContent: true))" /> <set-variable name="jsonRpcMethod" value="@( var body = context.Variables["jsonRpcBody"]; var jsonObj = JsonDocument.Parse(body); return jsonObj.RootElement.GetProperty("method").GetString(); )" /> <!-- Step 3: Conditional Authorization Based on Method --> <choose> <when condition="@(context.Variables["jsonRpcMethod"] == "method.read")"> <!-- Authorize Read Method --> <check-claim name="scp" failed-check-httpcode="403" failed-check-error-message="Forbidden. The required scope for read method is missing." match="any"> <value>method.read</value> </check-claim> </when> <when condition="@(context.Variables["jsonRpcMethod"] == "method.write")"> <!-- Authorize Write Method --> <check-claim name="scp" failed-check-httpcode="403" failed-check-error-message="Forbidden. The required scope for write method is missing." match="any"> <value>method.write</value> </check-claim> </when> <when condition="@(context.Variables["jsonRpcMethod"] == "method.admin")"> <!-- Authorize Admin Method --> <check-claim name="roles" failed-check-httpcode="403" failed-check-error-message="Forbidden. You do not have the required admin role." match="any"> <value>Admin</value> </check-claim> </when> <otherwise> <!-- Default Forbidden if method is not recognized --> <return-response response-message="Forbidden. The method is not recognized." status-code="403" /> </otherwise> </choose>
- Azure B2C配置:已配置包含应用注册及注册登录流程的IEF自定义策略(改编自微软示例,可正常运行),但对策略细节功能了解有限。
未解决问题
- 不知如何扩展SelfAsserted声明以添加自定义属性
- 不知如何在令牌中传递包含授权信息的额外声明
核心疑问
- 当前方案是否正确?该流程比预期复杂,是否为Azure生态下的常规实现方式?
- 是否存在更简洁的替代方案?
内容的提问来源于stack exchange,提问作者mjmar-01
相关产品推荐
相关产品推荐

