如何解决使用SAS访问Azure Event Hubs时的401授权错误?
Azure Event Hubs发送消息时401未授权问题排查
我尝试使用以下代码向Azure Event Hubs发送消息:
var producer = new EventHubProducerClient( ConfigurationManager.AppSettings["EventHubConnectionString"], ConfigurationManager.AppSettings["EventHubName"] );
我确认SAS密钥(连接字符串)是正确的,按照Azure.Messaging.EventHubs.Producer库的要求使用了实例级(也试过命名空间级)密钥,但仍返回如下错误:
---> System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.WebRequest.<>c.<GetResponseAsync>b__68_2(IAsyncResult iar) at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)...
排查建议
- 检查连接字符串中的EntityPath是否与Event Hub实例名称完全匹配,注意避免空格、大小写或拼写错误
- 确认SAS密钥对应的共享访问策略已勾选Send权限,无此权限会直接导致授权失败
- 检查本地系统时间是否与Azure服务器时间同步,SAS签名依赖时间戳,时间差超过15分钟会触发401错误
- 升级Azure.Messaging.EventHubs.Producer库到最新稳定版本,旧版本可能存在协议或签名逻辑的兼容性问题
- 尝试重新生成SAS密钥并等待1-2分钟后再测试,部分场景下密钥生效存在延迟
内容的提问来源于stack exchange,提问作者coding99
相关产品推荐
相关产品推荐

