C# 在类库中访问会话。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11930829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:45:32  来源:igfitidea点击:

Access session in class library.

c#asp.net

提问by Bikash Shrestha

I am developing an application architecture that uses 2 sub projects: a) asp.net web application (it covers user interface and business logic) and b) class library. (it covers data access layer)

我正在开发一个使用 2 个子项目的应用程序架构:a) asp.net web 应用程序(它涵盖用户界面和业务逻辑)和 b) 类库。(它涵盖了数据访问层)

After system user successfully logs in , the user information is stored in a session object.

系统用户登录成功后,用户信息保存在一个会话对象中。

The problem I am facing is when I try to access that session object in class library project(data access layer), it always returns null.

我面临的问题是当我尝试访问类库项目(数据访问层)中的会话对象时,它总是返回 null。

I need to access the session object in class library project because, in my case each user has their own username and password for database access(for security reasons);

我需要访问类库项目中的会话对象,因为在我的情况下,每个用户都有自己的数据库访问用户名和密码(出于安全原因);

So, How how do i read and write from/to session object in class library project

那么,我如何在类库项目中读写会话对象

回答by geekchic

Use the System.Web.HttpContext.Current.Sessionobject.

使用System.Web.HttpContext.Current.Session对象。

回答by Giedrius

First of all, as Peri correctly noticed - you need to think again if having separate database logins for each user is a good idea - because you loose connection pooling (different users won't be able to reuse existing connections - and creating a new sql connection is quite expensive).

首先,正如 Peri 正确注意到的那样-您需要重新考虑是否为每个用户使用单独的数据库登录是个好主意-因为您失去了连接池(不同的用户将无法重用现有连接-并创建一个新的 sql连接相当昂贵)。

If you really wish to keep separate database users, I would create interface to abstract session from data access:

如果您真的希望保留单独的数据库用户,我将创建接口以从数据访问中抽象会话:

public interface ILoginDataService
{
   LoginData Current { get; }
}

And implementation would pass login data from session. In such way you won't have session dependency to session in your data access logic - so it will be more testable, also you'll separate concerns.

并且实现将从会话传递登录数据。通过这种方式,您的数据访问逻辑中不会有会话依赖于会话 - 因此它将更易于测试,您也将分离关注点。

回答by user1496412

Here is the code I used within a library to get session information.

这是我在库中用于获取会话信息的代码。

public static string Entity()
{
    string entity = "";
    HttpContext httpContext = HttpContext.Current;
    if (httpContext.ApplicationInstance.Session.Count > 0)
        entity = httpContext.ApplicationInstance.Session["EntityCode"].ToString();

    return entity;
}

回答by FaheemMCFC

I am having an ASP.Net application which uses session. I am able to access it in my app_code files using [WebMethod(EnableSession = true)] for the function. I am not sure whether this is your problem. I also faced session value as null when I removed (EnableSession = true) on the method.

我有一个使用会话的 ASP.Net 应用程序。我可以在我的 app_code 文件中使用 [WebMethod(EnableSession = true)] 来访问它。我不确定这是否是您的问题。当我在方法上删除 (EnableSession = true) 时,我还面临会话值为 null。

回答by Ahmed Mohammed Fahmy

using System.Web;

namespace ClassNameSpace
{
    public class Class1 : IRequiresSessionState
    {
            private string sessionValue => HttpContext.Current.Session["sessionKey"].ToString();
    }
}