C# 在会话期间在业务逻辑层中存储用户登录数据

C# 在会话期间在业务逻辑层中存储用户登录数据,c#,asp.net,asp.net-mvc,session,asp.net-core,C#,Asp.net,Asp.net Mvc,Session,Asp.net Core,我有一个与dll相关的项目,如下所示: DataAccessLayerDAL我建议您创建一个上下文对象,其中包含一个可以注入DAL的接口。这样,DAL将取决于您的接口,而不是任何特定于web的API 比如说 interface IDatabaseAuthenticationContext { string DatabaseUserName { get; set; } string DatabasePassword { get; set ; } } class Authentic

我有一个与dll相关的项目,如下所示:
DataAccessLayerDAL我建议您创建一个上下文对象,其中包含一个可以注入DAL的接口。这样,DAL将取决于您的接口,而不是任何特定于web的API

比如说

interface IDatabaseAuthenticationContext
{
    string DatabaseUserName { get; set; }
    string DatabasePassword { get; set ; }
}

class AuthenticationContext: IDatabaseAuthenticationContext
{
    private readonly HttpContextBase _httpContext;

    public AuthenticationContext(HttpContextbase httpContext)
    {
        _httpContext = httpContext;
    }

    public string DatabaseUserName
    {
        get
        {
            return _httpContext.Session["DatabaseUserName"];
        }
        set
        {
            _httpContext.Session["DatabaseUserName"] = value;
        }
    }
    public string DatabasePassword
    {
        get
        {
            return _httpContext.Session["DatabasePassword"];
        }
        set
        {
            _httpContext.Session["DatabasePassword"] = value;
        }
    }
}
然后在您的DAL中:

class DataAccessLayer : IDataAccessLayer
{
    private readonly IDatabaseAuthenticationContext _dbAuthenticationContext;

    public DataAccessLayer(IDatabaseAuthenticationContext context)
    {
        _dbAuthenticationContext = context; //Injected
    }

    public void ExecuteSomeCommand()
    {
        using (var conn = new SqlConnection(this.CreateConnectionString()))
        {
            var cmd = new SqlCommand("SomeCommand");
            cmd.CommandType = StoredProcedure;
            cmd.Connection = conn;
            cmd.ExecuteNonQuery();
        }
    }

    private string CreateConnectionString()
    {
        return String.Format("Server={0};UID={1};PWD={2}", this.GetServerName(),
                                                           _dbAuthenticationContext.DatabaseUserName,
                                                           _dbAuthenticationContext.Databasepassword);
    }
然后在你的作文根中:

container.RegisterType<IDatabaseAuthenticationContext, AuthenticationContext>();
container.RegisterType<HttpContextBase, () => new HttpContextWrapper(HttpContext.Current));
container.RegisterType<IDataAccessLayer, DataAccessLayer>();

通过这种方式,您可以将所需内容传递给DAL,而不需要DAL了解任何关于HttpContext或其他特定于web的API的信息。如果您在编写控制台应用程序或其他程序时未引用System.Web,则只需编写实现IDatabaseAuthenticationContext的AuthenticationContext的不同实现。

对于类似的内容,您可以使用工厂或类似于传入连接字符串的新存储库?通过这种方式,唯一的直接依赖关系是一个简单的字符串,您可以将它传递到数据访问层,并处理为用户获取正确的repo的问题。应用程序本质上只需要一个到数据库的连接,以及该应用程序的凭据。您的用户没有连接到数据库,您的应用程序正在连接。用户身份验证和授权是您的应用程序需要处理的事情,而不是数据库。是每个用户都有一个连接字符串,还是每个租户都有一个数据库,每个租户都有一个用户池?如果是这样的话,可能会有更好的解决方案,比如根据租户标识符解析正确的连接?这必须和我描述的一样,每个用户都必须有自己的连接字符串。网站基本上是现有数据库端解决方案的用户界面,并没有其他选择。
container.RegisterType<IDatabaseAuthenticationContext, AuthenticationContext>();
container.RegisterType<HttpContextBase, () => new HttpContextWrapper(HttpContext.Current));
container.RegisterType<IDataAccessLayer, DataAccessLayer>();