我想存储user
globaly
的登录信息。如果用户已经登录,那么如果another user
想要从另一台PC登录,那么应用程序必须警告他另一个user
也使用此name
登录。
我不想使用DataBase
。我认为HttpApplication
是一个解决方案。但我无法理解它的流程。
我所知道的是,我必须在Global.asax.cs
文件中为其指定值:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
Application["Username"] = "Test";
}
}
现在我想在controller
Login Function
中访问它,我会检查Application
是否有某些数据。
如果是null
,则表示没有user
登录。但我不明白如何访问HTTPApplication
对象以及如何将username
分配给它在Application_Start()
。
答案 0 :(得分:2)
在控制器中,您可以访问应用程序对象。即
public ActionResult Index()
{
string value = HttpContext.Application["a"] as string;
if (value == null){
value = DateTime.Now.ToLongTimeString();
HttpContext.Application["a"] = value;
}
return Content(value);
}
答案 1 :(得分:0)
试试这个,
string value = HttpContext.Current.Application["a"] as string;