You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过SDK交互式认证Azure?F#实践报错求助

在F#中实现Azure交互式认证的解决方案

问题概述

你尝试用DefaultAzureCredential替代过时的Gist代码实现Azure认证,在VS Code运行F#代码时遭遇System.MissingMethodException,提示找不到Azure.Core.ClientOptions..ctor(Azure.Core.DiagnosticsOptions)方法。

错误原因

这是Azure.Identity与Azure.Core版本不兼容导致的:你使用的Azure.Identity 1.6.1依赖的Azure.Core版本远低于当前绑定的1.23.0,版本不匹配引发构造方法调用失败。

解决方案

1. 升级Azure.Identity到兼容版本

先在代码中指定较新的、与Azure.Core兼容的Azure.Identity稳定版本,比如1.12.0:

#r "nuget: Azure.Identity, 1.12.0"
open Azure.Identity

2. 实现交互式认证的两种方式

方式一:使用DefaultAzureCredential自动适配(推荐本地开发)

DefaultAzureCredential会自动尝试多种认证方式,包括本地交互式浏览器认证,无需配置托管标识参数(ManagedIdentityClientId是给云环境托管标识用的,本地开发不需要):

// 自动触发本地交互式浏览器认证
let credential = DefaultAzureCredential()

运行代码后会弹出浏览器,引导你完成Azure账号登录。

方式二:明确使用InteractiveBrowserCredential

如果需要指定租户ID或客户端ID,直接用InteractiveBrowserCredential:

open Azure.Core

let authOptions = InteractiveBrowserCredentialOptions(
    TenantId = "你的tenant_id",
    ClientId = "你的client_id"
)
let credential = InteractiveBrowserCredential(authOptions)

3. 验证认证有效性

可以调用Azure服务API测试,比如获取订阅信息(需引入Azure.ResourceManager包):

#r "nuget: Azure.ResourceManager, 1.1.0"
open Azure.ResourceManager
open Azure.ResourceManager.Resources

let armClient = ArmClient(credential)
let subscription = armClient.GetSubscriptionResource(ResourceIdentifier("/subscriptions/你的subscription_id"))
let subscriptionDetails = subscription.GetAsync().Result
printfn "订阅名称: %s" subscriptionDetails.Data.DisplayName

内容的提问来源于stack exchange,提问作者Alex_P

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 16:06:25