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

前端之家收集整理的这篇文章主要介绍了c# – 在ASP.NET应用程序中获取当前用户名前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在运行一个需要能够读取当前用户登录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而不使用斜杠.

任何建议表示赞赏.

问候.

@H_502_16@

解决方法

尝试使用它来检索用户名….
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>
@H_502_16@ @H_502_16@ 原文链接:https://www.f2er.com/csharp/244128.html

猜你在找的C#相关文章