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

能否在.NET Core中使用Agora视频通话Web API SDK?如何实现?

是否可以在.NET Core开发环境中使用Agora视频通话Web API SDK

完全可以,根据使用场景分为两种落地方式:如果是后端服务对接Agora的服务端管控能力(生成Token、频道管理、通话质量查询等),直接通过HTTP请求调用Agora服务端REST API即可;如果是在Blazor等.NET Core前端框架中实现音视频通话的前端交互,也可以通过JS互操作调用Agora官方的Web JS SDK实现完整能力。

.NET Core项目集成Agora SDK的实操步骤

分两种常用场景说明:

场景1:ASP.NET Core后端集成Agora服务端能力

  • 前置准备:先注册Agora开发者账号,拿到项目的App ID、App Certificate、Customer ID、Customer Certificate四个核心凭证,存入项目的appsettings.json配置文件中,不要硬编码在业务代码里。
  • 核心逻辑开发:Agora所有服务端接口均为REST风格,直接使用.NET Core原生的HttpClient调用即可,不需要引入额外的第三方SDK。以最常用的RTC Token生成为例,可以直接使用Agora官方提供的.NET版本Token生成工具类,封装接口示例如下:
// appsettings.json配置示例
"AgoraConfig": {
  "AppId": "替换为你的Agora App ID",
  "AppCertificate": "替换为你的Agora App证书"
}

// 业务接口实现
[ApiController]
[Route("api/agora")]
public class AgoraTokenController : ControllerBase
{
    private readonly IConfiguration _config;
    private readonly IHttpClientFactory _httpClientFactory;

    public AgoraTokenController(IConfiguration config, IHttpClientFactory httpClientFactory)
    {
        _config = config;
        _httpClientFactory = httpClientFactory;
    }

    [HttpGet("generate-token")]
    public IActionResult GenerateRtcToken(string channelName, uint uid, int expireTime = 3600)
    {
        var appId = _config.GetValue<string>("AgoraConfig:AppId");
        var appCert = _config.GetValue<string>("AgoraConfig:AppCertificate");
        // 调用Agora官方RtcTokenBuilder生成Token
        var token = RtcTokenBuilder.BuildTokenWithUid(appId, appCert, channelName, uid, RtcTokenBuilder.Role.RolePublisher, expireTime);
        return Ok(new { Channel = channelName, Uid = uid, Token = token, ExpireTime = expireTime });
    }
}
  • 其他服务端操作:比如频道踢人、查询在线用户数、拉取通话质量统计数据等,只需要给HttpClient添加对应的鉴权头(将Customer ID和Customer Certificate拼接后做Base64编码放到Authorization头),按照接口文档组装请求参数即可调用。

场景2:Blazor项目集成Agora Web SDK实现前端音视频通话

如果是用Blazor做前端,可以通过.NET的JS互操作能力调用Agora Web JS SDK实现通话能力:

  • 第一步:在Blazor项目的入口HTML文件(WebAssembly项目是wwwroot/index.html,Blazor Server项目是Pages/_Layout.cshtml)中引入Agora Web SDK和自己封装的JS工具文件。
  • 第二步:在wwwroot/js目录下新建agoraOperate.js,封装Agora的基础操作逻辑:
let rtcClient = null;
let localAudioTrack = null;
let localVideoTrack = null;

window.AgoraOperate = {
    // 初始化RTC客户端
    initClient: async function (appId) {
        rtcClient = AgoraRTC.createClient({ mode: "rtc", codec: "vp8" });
        await rtcClient.initialize(appId);
        // 绑定远端用户发布流的监听事件
        rtcClient.on("user-published", async (user, mediaType) => {
            await rtcClient.subscribe(user, mediaType);
            if (mediaType === "video") {
                user.videoTrack.play("remote-player-container");
            }
            if (mediaType === "audio") {
                user.audioTrack.play();
            }
        });
    },
    // 加入频道
    joinChannel: async function (token, channelName, uid) {
        await rtcClient.join(token, channelName, uid);
        // 创建本地音视频轨道
        [localAudioTrack, localVideoTrack] = await AgoraRTC.createMicrophoneAndCameraTracks();
        // 播放本地视频
        localVideoTrack.play("local-player-container");
        // 发布本地流
        await rtcClient.publish([localAudioTrack, localVideoTrack]);
    }
}
  • 第三步:在Blazor组件中通过IJSRuntime调用封装好的JS方法即可:
@page "/agora-call"
@inject IJSRuntime JSRuntime
@inject HttpClient HttpClient

<h3>Agora视频通话测试</h3>
<div>
    <div id="local-player-container" style="width: 300px; height: 200px; display: inline-block; margin-right: 20px;"></div>
    <div id="remote-player-container" style="width: 300px; height: 200px; display: inline-block;"></div>
</div>
<button class="btn btn-primary mt-3" @onclick="StartCall">开始通话</button>

@code {
    private const string AppId = "替换为你的Agora App ID";
    private const string TestChannel = "test_channel_001";
    private const uint TestUid = 10001;

    private async Task StartCall()
    {
        // 先调用后端接口获取Token
        var tokenRes = await HttpClient.GetFromJsonAsync<TokenRes>($"api/agora/generate-token?channelName={TestChannel}&uid={TestUid}");
        // 初始化Agora客户端
        await JSRuntime.InvokeVoidAsync("AgoraOperate.initClient", AppId);
        // 加入频道开始通话
        await JSRuntime.InvokeVoidAsync("AgoraOperate.joinChannel", tokenRes.Token, TestChannel, TestUid);
    }

    public class TokenRes
    {
        public string Token { get; set; }
    }
}

注意:生产环境下禁止将App Certificate、Customer Certificate等敏感凭证暴露在前端代码中,所有涉及凭证的操作都必须放在后端服务处理,避免敏感信息泄露引发安全问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 10:09:08