.NET6 WebApi 配置带密码的Redis

.NET6 WebApi 配置带密码的Redis,第1张

先在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<MyDbContext>();
builder.Services.AddTransient<IModelRepository, ModelRepository>();
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<IDistributedCacheHelper, DistributedCacheHelper>();

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 Files\Redis

管理员权限打开 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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/741842.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存