gpt4 book ai didi

c# - 如何让当前用户登录到 Windows?

转载 作者:太空宇宙 更新时间:2023-11-03 21:24:24 25 4
gpt4 key购买 nike

我是一个事件目录用户,我只是想在 Respnse.Write() 方法中打印出当前用户的名称。从我从这里发布的其他几个问题中读到的内容,我需要使用

using System.Security.Principal;
string username = WindowsIdentity.GetCurrent().Name

但是,当我尝试将用户名写入屏幕时,我得到了

NT AUTHORITY\网络服务

代替

域\12345678

这是我用来写入屏幕的代码:

Response.Write(WindowsIdentity.GetCurrent().Name);

并且我在我的 web.config 中将 identity impersonate 设置为 true。接下来我该做什么?

经过编辑以显示建议的答案

我的页面加载

protected void Page_Load(object sender, EventArgs e)
{
string userName = User.Identity.Name;
Response.Write(userName);
//currently returning null
}

最佳答案

在您的 web.config 中,您需要将 身份验证模式 切换到 Windows 并且您需要禁用匿名用户

<system.web>
<authentication mode="Windows" />
<anonymousIdentification enabled="false" />
<authorization>
<deny users="?" />
</authorization>
</system.web>

关于c# - 如何让当前用户登录到 Windows?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28155069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com