gpt4 book ai didi

c# - 在 ASP.NET 应用程序中获取当前用户名

转载 作者:太空狗 更新时间:2023-10-29 20:10:54 26 4
gpt4 key购买 nike

我正在运行一个需要能够读取当前用户的登录 ID 的网页。这是我正在使用的代码:

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

目前这会返回正确的登录名,但是当我在此方法中使用它时:

protected Boolean isPageOwner()
{
string id = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
alert("User: " + id);
if (id.Equals(pageOwnerID))
{
return true;
}
if (accessPermission.ContainsKey(id))
{
return true;
}
return false;
}

即使返回的 id 与 pageOwnerID 相同,该方法也会返回 false。我真的不确定我的哪一部分有问题。

附带说明一下,我的登录 ID 的格式为 string1/string2,但代码将其检索为 string1 + string2,不带斜线。

如有任何建议,我们将不胜感激。

问候。

最佳答案

尝试使用它来检索用户名....

if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
string username = System.Web.HttpContext.Current.User.Identity.Name;
}

听起来好像没有使用 Windows 身份验证 - 您需要禁用匿名访问并启用 Windows 集成安全性。

将此添加到您的 web.config...

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

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

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