c# - 如何获取正在访问 ASP.NET 应用程序的当前用户?

标签 c# asp.net windows-server-2008

要获取系统中的当前登录用户,我使用以下代码:

string opl = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

我在 ASP.NET 应用程序上工作,我需要这些信息。所以我把我的应用程序放在服务器上并尝试了上面的代码,我在字符串 opl 中得到了“Network Service”。我需要知道访问我的 ASP.NET 应用程序的 PC 的当前用户。

最佳答案

快速的答案是 User = System.Web.HttpContext.Current.User

确保您的 web.config 具有以下身份验证元素。

<configuration>
    <system.web>
        <authentication mode="Windows" />
        <authorization>
            <deny users="?"/>
        </authorization>
    </system.web>
</configuration>

延伸阅读:Recipe: Enabling Windows Authentication within an Intranet ASP.NET Web application

关于c# - 如何获取正在访问 ASP.NET 应用程序的当前用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5417125/

相关文章:

asp.net - 如何在 ASP.NET 和 Tomcat 之间同步 session /安全?

asp.net - 在 Windows 2008 服务器上运行 aspnet_setreg.exe

c# - 使用代码切换到 TabControl 中的选项卡

c# - 从不同线程调用方法 AutoResetEvent 是否安全?

c# - ForeignKeyConstraint 要求子键值 (0) 存在于父表中

windows-server-2008 - 计划任务在 Windows Server 2008 R2 中不起作用

IIS 上的 PHP - 执行返回 1

c# - .net 电子邮件模板引擎

c# - 用于自动 UI 测试的模拟实现

c# - C# new 和 override 属性关键字之间的区别