[英]Session Start Global.asax C# ASP.NET
I have the following code: 我有以下代码:
protected void Session_Start(object sender, EventArgs e)
{
WindowsPrincipal p = Thread.CurrentPrincipal as WindowsPrincipal;
string sUserAccount = HttpContext.Current.User.Identity.Name.ToString();
HttpContext.Current.Session["WinAccount"] = sUserAccount;
}
The code is to get the windows user name. 该代码是获取Windows用户名。 From the session_start
, I want to create a session which called WinAccount
. 从session_start
,我想创建一个名为WinAccount
的会话。 But, when I tried to call the session from one of my page (default.aspx) which is has master page
on it. 但是,当我尝试从页面之一(default.aspx)调用会话时,该页面上有master page
。
Let say, on page_load: 假设在page_load上:
string sWinAccount = Session["WinAccount"].ToString();
Label1.Text = sWinAccount.ToString();
The web.config
looks like: web.config
看起来像:
<authentication mode="Windows"/>
<identity impersonate="false"/>
<authorization>
<deny users="?"/>
</authorization>
Also, the properties of the project has been enabling the windows authentication mode. 此外,项目的属性已启用Windows身份验证模式。
When I run, it blanks. 当我跑步时,它一片空白。 Please advise. 请指教。
Thank you. 谢谢。
尝试
string sUserAccount =System.Security.Principal.WindowsIdentity.GetCurrent().Name.Tostring();
Session_Start event fired when a new client start their very first request to the app, not when the user is logged in. So in your case, the HttpContext.Current.User.Identity.Name
is empty at the time Session_Start is called. 当新客户端向应用程序发起其第一个请求时(而不是在用户登录时)触发Session_Start事件。因此,在您的情况下,在调用Session_Start时HttpContext.Current.User.Identity.Name
为空。 It worked as expected. 它按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.