我在登录时创建了一个会话,在该会话中我放置了用户对象。
public sealed class MyContext
{
[ThreadStatic]
private static MyContext staticContext = null;
public HttpContext Context
{
get
{
return HttpContext.Current;
}
}
private MyContext()
{
}
public static MyContext Current
{
get
{
MyContext myContext = null;
HttpContext context = HttpContext.Current;
if (context != null)
{
//it in the Items collection (a per request store)
if (context.Items.Contains("CONTEXT_KEY"))
myContext = context.Items["CONTEXT_KEY"] as MyContext;
else
{
myContext = new MyContext();
myContext.Context.Items["CONTEXT_KEY"] = myContext;
}
}
else if (staticContext != null)
{
myContext = staticContext;
}
else
{
//create a new instance of static context as there is no web request
staticContext = new MyContext();
myContext = staticContext;
}
return myContext;
}
}
public MyUser User
{
get
{
MyUser user = null;
if (Current.Context.Session["LoggedInUser"] != null)
user = (MyUser)Current.Context.Session["LoggedInUser"];
else
{
user = UserDetail.GetUser(HttpContext.Current.User.Identity.Name);
if (user == null)
{
//if there is no userID, create anonymous user
user = new MyUser();
user.IsAnonymous = true;
user.UserID = 0;
}
//cache in session
Current.Context.Session["User"] = user;
}
return user;
}
set
{
Current.Context.Session["User"] = value;
}
}
}//end class
现在我在整个应用程序中使用这个上下文,如果用户登录并且同时管理员停用该用户,我无法停止用户活动,这是因为用户的信息在会话中。因此,请建议我在管理员停用该用户时如何更新该特定用户的信息。