将UserID和Username存储为全局变量

时间:2014-11-20 01:18:52

标签: c# asp.net global-variables userid

我是ASP.NET的新手,所以请在回复中考虑到这一点。

我有一个方法可以为我的用户名和用户ID创建一个会话cookie,当我将其放入后面的代码中时(见下文)

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.User.Identity.IsAuthenticated) // if the user is already logged in
    {
        MembershipUser currentUser = Membership.GetUser();

        Guid CurrentUserID = (Guid)currentUser.ProviderUserKey;
        string CurrentUsername = (string)currentUser.UserName;

        Session["CurrentUserID"] = CurrentUserID;
        Session["CurrentUserName"] = CurrentUsername;
    }
    else
    {
        Session["CurrentUserID"] = "";
        Session["CurrentUserName"] = "";
    }
}

我正在尝试清理我的项目,并认为将任何方法存储到我的App_code目录中的类文件中是明智的,这样我只有一个实例。

我不能将上面的代码剪切并粘贴到类文件(下面)中,因为我收到了多个错误。

我想知道将这些存储为全局变量的最佳做法是什么?

我的App_code文件夹中的我的班级文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

/// <summary>
/// Generic utilities that can be accessed from any page
/// </summary>
public static class GlobalUtilities
{


    //Takes x characters from the right hand side. TO USE: MyString.TxtStrRight(8)
    public static string TxtStrRight(this string value, int length)
    {
        if (String.IsNullOrEmpty(value)) return string.Empty;

        return value.Length <= length ? value : value.Substring(value.Length - length);
    }

    //Takes x characters from the left hand side. TO USE: MyString.TxtStrLeft(40)
    public static string TxtStrLeft(this string value, int length)
    {
        if (String.IsNullOrEmpty(value)) return string.Empty;

        return value.Length <= length ? value : value.Substring(0, length) + "...";
    }

    //Get the difference between time and date of NOW and the database value. TO USE: GlobalUtilities.GetDiffDate(MyDate)
    public static string GetDiffDate(DateTime dt)
    {
        TimeSpan ts = dt - DateTime.Now;
        if (Math.Abs(ts.TotalHours) < 24 && Math.Abs(ts.TotalHours) >= 1)
        {
            return string.Format("{0:0} hrs ago", Math.Abs(ts.TotalHours));
        }
        else if (Math.Abs(ts.TotalHours) < 1)
        {
            return string.Format("{0:0} mins ago", Math.Abs(ts.TotalMinutes));
        }
        else
        {
            return dt.ToString("dd MMM yyyy");
        }
    }



}

1 个答案:

答案 0 :(得分:1)

Page_Load是一个事件处理程序,用于处理在ASP .Net Web请求的生命周期中发生的page.load事件。虽然它实际上是一种方法,但它不是GlobalUtilities类中的扩展方法的意义上的方法。如果你试图从触发Page.Load事件的事物的上下文中取出该方法,它将无法工作。

如果您的目标是抽象出设置会话变量的代码,以避免重复,您可以创建一个不同的方法来明确处理它。

可能看起来像这样:

public static void SetSessionVariables(this MembershipUser currentUser) {
    Guid CurrentUserID = (Guid)currentUser.ProviderUserKey;
    string CurrentUsername = (string)currentUser.UserName;

    Session["CurrentUserID"] = CurrentUserID;
    Session["CurrentUserName"] = CurrentUsername;
}

然后你的Page_Load处理程序将如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.User.Identity.IsAuthenticated) { // if the user is already logged in
        MembershipUser currentUser = Membership.GetUser();

        currentUser.SetSessionVariables();
    }
    else
    {
        Session["CurrentUserID"] = "";
        Session["CurrentUserName"] = "";
    }
}

仍然会留下一些东西。如果不能移动事件处理程序本身,则无法将初始化移出事件处理程序;在此上下文之外,您无法使用Page对象。

就存储全局数据的最佳方式而言,Session是一个非常好的选择。唯一的缺点是Session本身是HttpContext的属性;所以它不会在你的表示层(web项目)之外可用,除非你在方法之间传递上下文,那很糟糕。