Session 在一个类中设置会话变量就是设置所有变量,而不仅仅是一个

Session 在一个类中设置会话变量就是设置所有变量,而不仅仅是一个,session,session-variables,Session,Session Variables,我一直在研究一种方法,为我的Web应用程序设置会话变量,以存储登录用户的信息。作为一名新手,我已经取得了很大的进步,但我很困惑,为什么设置变量会设置所有变量,而不仅仅是指定的变量。有人能指出我的错误吗?谢谢 using System; using System.DirectoryServices; using System.Security.Principal; using System.Web; using System.Web.UI; namespace ITHD { public cla

我一直在研究一种方法,为我的Web应用程序设置会话变量,以存储登录用户的信息。作为一名新手,我已经取得了很大的进步,但我很困惑,为什么设置变量会设置所有变量,而不仅仅是指定的变量。有人能指出我的错误吗?谢谢

using System;
using System.DirectoryServices;
using System.Security.Principal;
using System.Web;
using System.Web.UI;

namespace ITHD
{
public class Common : Page
{

    public static void GetLoggedInUserProperties()
    {
        string gLoginId = ExtractUserName(WindowsIdentity.GetCurrent().Name);
        SessionManager.LoginId = gLoginId;

        VerifyInAD(gLoginId);

    }
    private static void VerifyInAD(string sUser)
    {
        try
        {
            string userName = ExtractUserName(sUser);
            DirectorySearcher search = new DirectorySearcher();

            search.Filter = String.Format("(SAMAccountName={0})", userName);

            search.PropertiesToLoad.Add("cn");
            search.PropertiesToLoad.Add("MemberOf");
            search.PropertiesToLoad.Add("givenname");
            search.PropertiesToLoad.Add("sn");
            search.PropertiesToLoad.Add("phone");
            search.PropertiesToLoad.Add("title");

            SearchResult result = search.FindOne();

            //SessionManager.CanEdit = "False";
            SessionManager.UserName = string.Empty;

            if (result != null)
            {
                SessionManager.UserName = string.Format("{0}", result.Properties["cn"][0].ToString());
                bool admin = checkGroup("Helpdesk Agents");
                if (admin == true)
                {
                    SessionManager.HDAgent = "True";
                }
            }
            search.Dispose();
        }
        catch (Exception ex)
        {
            SessionManager.UserName = "Guest";
        }

    }

    public static string ExtractUserName(string path)
    {
        string[] userPath = path.Split(new char[] { '\\' });
        return userPath[userPath.Length - 1];
    }
    public static bool checkGroup(string group)
    {
        WindowsIdentity identity = WindowsIdentity.GetCurrent();
        WindowsPrincipal principal = new WindowsPrincipal(identity);
        return principal.IsInRole(group);
    }
}

public static class SessionManager
{
    private const string sLoginId = "";
    public static string LoginId
    {
        get
        {
            if (null != HttpContext.Current.Session[sLoginId])
                return HttpContext.Current.Session[sLoginId] as string;
            else
                return "Guest";
        }
        set
        {
            HttpContext.Current.Session[sLoginId] = value;
        }
    }
    private const string sUserName = "";
    public static string UserName
    {
        get
        {
            if (null != HttpContext.Current.Session[sUserName])
                return HttpContext.Current.Session[sUserName] as string;
            else
                return "Guest";
        }
        set
        {
            HttpContext.Current.Session[sUserName] = value;
        }
    }
    private const string sHDAgent = "";
    public static string HDAgent
    {
        get
        {
            if (null != HttpContext.Current.Session[sHDAgent])
                return HttpContext.Current.Session[sHDAgent] as string;
            else
                return "False";
        }
        set
        {
            HttpContext.Current.Session[sHDAgent] = value;
        }
    }
}

}我认为您没有正确设置会话密钥。在静态类中,有一个属性设置器,它本质上是会话[“”]=所有成员的某个值。在.net 3.5中,您不需要为私有成员的名称设置密钥,甚至不需要私有成员。您也可以只填充整个会话对象,而不必担心每个成员

然后只需访问属性的当前实例化器,如中所示

MySession.Current.UserName="guest";


public class MySession
{
    private const string _SessionName = "__MY_SESSION__";
    //--------------------------------------------------------------------------------------------
    private MySession(){}
    //--------------------------------------------------------------------------------------------
    public static MySession Current
    {
        get
        {
            MySession session =
                (MySession)HttpContext.Current.Session[_SessionName];
            if (session == null)
            {
                session = new MySession();
                session.Property1 = new Property1();
                session.Property2 = new Property2();
                session.UserName = "";
                HttpContext.Current.Session[_SessionName] = session;
            }
            return session;
        }
    }
    public string UserName { get; set; }
    public Property1 Property1 { get; set; }
    public Property2 Property2 { get; set; }
}

我已经实现了这段代码,效果非常好!谢谢我在“session.Property1=newproperty1();”部分有点纠结,因为我以前没有这样做过,所以如果可以解释这个部分,这样我就可以清除“'ITHD.MySession.Property1'是一个'property',但像'type'一样使用”错误,那就太好了。不管怎样,我都会研究它。谢谢你的帮助!我将使用当前属性访问每个属性,以便获得对单个应用程序范围的MySession的引用,从而更新底层会话。。。Property1 something=ITHD.MySession.Current.Property1;然后,对某些内容的更改将通过引用更新会话中的任何内容。