栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

.NET6 WebApi 配置带密码的Redis

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.NET6 WebApi 配置带密码的Redis

先在appsettings里配置:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "ConnectionStrings": {
    "Redis": "127.0.0.1:6379" //redis 绑定的ip和端口
  },
  "AllowedHosts": "*"
}

using StackExchange.Redis;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

//builder.Services.AddScoped();
builder.Services.AddDbContext();
builder.Services.AddTransient();
builder.Services.AddStackExchangeRedisCache(opt =>
{
    var redisLocation = builder.Configuration.GetConnectionString("Redis");
    var redisOptions = ConfigurationOptions.Parse(redisLocation);
    redisOptions.Password = "123456";//你的redis密码
    opt.InstanceName = "cache2_"; //
    opt.ConfigurationOptions = redisOptions;
    //opt.Configuration = "localhost";
    //opt.InstanceName = "cache2_";
});
builder.Services.AddScoped();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();
app.Run();

修改redis密码:

打开
C:Program FilesRedis

管理员权限打开 redis-cli.exe

输入 set name “test1”

测试
get name

打开 redis.windows.conf
redis.windows-service.conf
搜索
requirepass
修改密码
requirepass 123456

打开
CMD

停止服务
redis-server.exe --service-stop
开启服务
redis-server.exe --service-start

进入redis
redis-cli.exe -h 127.0.0.1 -p 6379

验证
auth 123456

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/904857.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号