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

如何在C# StackExchange.Redis的Azure Redis连接字符串中指定只读用户?

Azure Redis + StackExchange.Redis 指定只读用户连接配置

连接字符串格式说明

StackExchange.Redis支持直接在连接字符串中嵌入只读用户的账号和密码,格式如下:

[只读用户名]:[只读用户密码]@[Redis实例主机名]:[端口],ssl=true

两种实现方式

  • 直接使用连接字符串
    把只读用户的账号密码直接拼入连接字符串,示例代码:

    // 替换为你的实际信息
    string redisConnection = "readonly_user:your_readonly_password@your_redis_name.redis.cache.windows.net:6380,ssl=true,abortConnect=false";
    ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(redisConnection);
    
  • 使用ConfigurationOptions构建(更灵活)
    如果需要动态配置参数,推荐用ConfigurationOptions类来设置,示例代码:

    var configOptions = new ConfigurationOptions
    {
        EndPoints = { "your_redis_name.redis.cache.windows.net:6380" },
        User = "readonly_user", // 指定只读用户名
        Password = "your_readonly_password", // 指定只读用户密码
        Ssl = true, // Azure Redis强制要求SSL
        AbortOnConnectFail = false // 避免初次连接失败直接终止程序
    };
    ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(configOptions);
    

注意事项

  • 确认Azure门户中已给该用户配置只读权限
  • 端口号默认是6380(SSL端口),不要用非SSL的6379端口
  • abortConnect=false参数可根据业务场景调整,若需要严格校验连接状态可设为true

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 13:02:02