加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 08:35:25 所属栏目:百科 来源:网络整理
导读:我正在运行一个需要能够读取当前用户的登录ID的网页.这是我正在使用的代码: string id = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 目前,这将返回正确的登录,但是当我在此方法中使用它时: protected Boolean isPageOwner(){ string i
我正在运行一个需要能够读取当前用户的登录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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读