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

时间:2014-11-10 16:46:20

标签: session session-variables

我一直在研究为我的Web App设置会话变量以存储登录用户信息的方法。我已经取得了很大的进步,作为一个新手,但我很难过为什么设置变量将设置所有这些而不仅仅是指定的变量。有人可以指出我的错误吗?谢谢!!

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;
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

我认为您没有正确设置会话密钥。在你的静态类中,你有一个属性设置器,它基本上是会话Session [“”] =所有成员的一些值。您不需要将密钥设置为.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; }
}