如何在新的Web应用程序中实现Global.asax中的User.Identity.Name

Kro*_*ian 5 c# asp.net

在Visual Studio中,我使用提供的模板创建了一个全新的ASP.NET Web窗体应用程序.

我没有对VS自动生成的代码做任何事情,除了将以下内容添加到Global.asax.cs

string myself = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
string me = User.Identity.Name;
Run Code Online (Sandbox Code Playgroud)

String我自己成功存储了当前的Windows登录ID.但是string me是null,因为当我调试时,User为null.但是,Visual Studio在调试之前未显示任何错误.

如何使用User.Identity.Name获取字符串的正确值?

Sam*_*hdi 9

Session_Start中的输入代码:

void Session_Start(object sender, EventArgs e) 
{
    string myself = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    string me = User.Identity.Name;
    Response.Write("myself:" + myself + "<br>me:" + me);
}
Run Code Online (Sandbox Code Playgroud)