Amazon ec2 如何在Elastic Beanstalk中跨多个EC2实例持久化.NET Core 3.1登录

Amazon ec2 如何在Elastic Beanstalk中跨多个EC2实例持久化.NET Core 3.1登录,amazon-ec2,.net-core,amazon-elastic-beanstalk,Amazon Ec2,.net Core,Amazon Elastic Beanstalk,我有一个小的.NET Core 3.1应用程序,需要偶尔扩展。我需要登录的用户信息在EB启动的任何实例中保持不变。我想使用我们已设置的SQL Server RDS来执行此操作。我已尝试添加分布式Sql Server缓存,并已设置用于存储sessionstate的表,如文档中所述。。。但是登录信息不会持久化到此表中 在my Startup.cs ConfigureServices中,我有: var sqlSessionConnString = new SqlConnectionStringBuil

我有一个小的.NET Core 3.1应用程序,需要偶尔扩展。我需要登录的用户信息在EB启动的任何实例中保持不变。我想使用我们已设置的SQL Server RDS来执行此操作。我已尝试添加分布式Sql Server缓存,并已设置用于存储sessionstate的表,如文档中所述。。。但是登录信息不会持久化到此表中

在my Startup.cs ConfigureServices中,我有:

var sqlSessionConnString = new SqlConnectionStringBuilder(Configuration.GetConnectionString("SqlSession"));
services.AddDistributedSqlServerCache(options =>
{
    options.ConnectionString = sqlSessionConnString.ConnectionString;
    options.SchemaName = "dbo";
    options.TableName = "TableName";
});
services.AddSession();
当我登录并检查表时,表中没有数据,但我的登录仍然有效


要告诉Identity将登录信息保存在数据库中而不是服务器内存中,这样无论用户被路由到哪个实例,我的登录都会被保存下来,我该怎么做?

回答您的问题:要配置会话,您还需要在配置方法中添加中间件app.UseSession()用法,以便

public void ConfigureServices(IServiceCollection services)
        {
            services.AddDistributedSqlServerCache(options =>
            {
                options.ConnectionString =
                    @"Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;";
                options.SchemaName = "dbo";
                options.TableName = "TestCache";
            });
            services.AddSession();
            //to inject httpcontet into controller
            services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            services.AddControllers();
        }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseSession();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseRouting();

            //your auth settings
            //...

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
或仅通过会话将数据存储到特定用途

_context.Session.SetString("name", "John");
这里有一个具体的例子

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
    private readonly IDistributedCache _distributedCache;
    private HttpContext _context;

    public WeatherForecastController(ILogger<WeatherForecastController> logger,
        IDistributedCache distributedCache,
        IHttpContextAccessor httpContextAccessor)
    {
        _distributedCache = distributedCache;
        _context = httpContextAccessor.HttpContext;
    }

    public string Get()
    {
        _distributedCache.SetString("TestString", "TestValue");
        if (_context.Session.Keys.Contains("name"))
            return $"Hello {_context.Session.GetString("name")}";  

        _context.Session.SetString("name", "John");
        return "Session was created";
    }
}
[ApiController]
[路线(“[控制器]”)]
公共类WeatherForecastController:ControllerBase
{
专用只读IDistributedCache _distributedCache;
私有HttpContext\u上下文;
公共天气预报控制器(ILogger logger,
IDistributedCache分布式缓存,
IHttpContextAccessor(httpContextAccessor)
{
_distributedCache=distributedCache;
_context=httpContextAccessor.HttpContext;
}
公共字符串Get()
{
_distributedCache.SetString(“TestString”、“TestValue”);
if(_context.Session.Keys.Contains(“name”))
返回$“Hello{{u context.Session.GetString(“name”)}”;
_context.Session.SetString(“name”、“John”);
返回“会话已创建”;
}
}
您将看到,内部SQL表数据将被插入

已创建和会话cookies(.AspNetCore.session)

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
    private readonly IDistributedCache _distributedCache;
    private HttpContext _context;

    public WeatherForecastController(ILogger<WeatherForecastController> logger,
        IDistributedCache distributedCache,
        IHttpContextAccessor httpContextAccessor)
    {
        _distributedCache = distributedCache;
        _context = httpContextAccessor.HttpContext;
    }

    public string Get()
    {
        _distributedCache.SetString("TestString", "TestValue");
        if (_context.Session.Keys.Contains("name"))
            return $"Hello {_context.Session.GetString("name")}";  

        _context.Session.SetString("name", "John");
        return "Session was created";
    }
}