如何从Cookies / Session

时间:2019-06-30 10:12:51

标签: c# asp.net-mvc entity-framework database-connection dbcontext

我有多个用户,他们拥有自己的数据库相同架构,因此我们将其ConnectionString存储在cookie / session中,该解决方案将可用。

如何使用HttpContext。 Cookie /会话数据可按每个请求更改DBContext的连接字符串?

  

数据库上下文

public MyContext(string ConnectionString) : base(ConnectionString)
{

}
  

DependencyRegistrar

  public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
    {
    //data layer
        var dataSettingsManager = new DataSettingsManager();
        var dataProviderSettings = dataSettingsManager.LoadSettings();
        builder.Register(c => dataSettingsManager.LoadSettings()).As<DataSettings>();
        builder.Register(x => new EfDataProviderManager(x.Resolve<DataSettings>())).As<BaseDataProviderManager>().InstancePerDependency();

        builder.Register(x => x.Resolve<BaseDataProviderManager>().LoadDataProvider()).As<IDataProvider>().InstancePerDependency();

        if (dataProviderSettings != null && dataProviderSettings.IsValid())
        {
            var efDataProviderManager = new EfDataProviderManager(dataSettingsManager.LoadSettings());
            var dataProvider = efDataProviderManager.LoadDataProvider();
            dataProvider.InitConnectionFactory();

            builder.Register<IDbContext>(c => new DBObjectContext(dataProviderSettings.DataConnectionString)).InstancePerLifetimeScope();
        }
        else
            builder.Register<IDbContext>(c => new DBObjectContext(dataSettingsManager.LoadSettings().DataConnectionString)).InstancePerLifetimeScope();

        //repositories
        builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();

1 个答案:

答案 0 :(得分:1)

您应该可以替换:

从这里: @Component({ selector: 'parent', template: ` <div>Parent yourSharedVariableParent: {{yourSharedVariableParent}}</div> <yourChild [(yourSharedVariable)]="yourSharedVariableParent"></yourChild> `, directives: [yourChildComponent] }) export class ParentComponent { yourSharedVariableParent ='hello'; constructor() { console.clear(); } }

dataProviderSettings.DataConnectionString

带有一个将从cookie读取值的类。

该新类显然不是控制器,因此需要通过builder.Register<IDbContext>(c => new DBObjectContext(dataProviderSettings.DataConnectionString)).InstancePerLifetimeScope();来访问HttpContext

为此,请添加以下内容:

IHttpContextAccessor

然后在您的新课程中:

public void ConfigureServices(IServiceCollection services)
{
     //other registrations
     services.AddHttpContextAccessor();
}

然后最后:

    public class ConnectionStringAccessor {
        private IHttpContextAccessor _httpContextAccessor;
        public ConnectionStringAccessor(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        public string GetConnectionString() {
            return  _httpContextAccessor.HttpContext.Request.Cookies["cookieName"].Value;
//you can consider some encryption/decryption or whatever you need to.
        }
    }

目前我没有Visual Studio,因此我没有构建或测试代码。另外,您的新课程必须在DI中注册。

有关更多信息,请参阅本文:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-2.2

正如我在评论中所说,只是加强那种处理诸如连接字符串之类的明智事情的方法是非常不安全的。