【问题标题】:How to access network id in asp.net 4.0如何在 asp.net 4.0 中访问网络 ID
【发布时间】:2016-09-30 04:58:09
【问题描述】:

我在 .Net framework 4.0 中使用 Default.aspx 页面创建了一个测试 Web 应用程序。我只想访问用户的网络 ID,所以我在后面的 default.aspx 代码中编写了以下代码,但是当我按 F5 运行应用程序时得到空白值:

string username = Page.User.Identity.Name;

并在 web.config 中启用 Windows 身份验证模式:

<authentication mode="Windows" />

然后我尝试在控制台应用程序中进行以下操作,我能够获取我的网络 ID:

Console.WriteLine(Environment.UserName);

我是否缺少 Web 应用程序中的任何配置步骤。

谢谢

【问题讨论】:

    标签: .net authentication windows-authentication asp.net-4.0


    【解决方案1】:

    这是我使用的生产代码,用于获取用户信息并检查 AD 组。

    创建一个变量,该变量将初始化类并访问您需要的任何方法,该方法将返回您的 AD \ Network 信息。

    using System.DirectoryServices.AccountManagement;
    using System.Security.Principal;
    
    public class IsUserInRole
    {
    
    public static bool IsInGroup(string groupName)
    {
        var myIdentity = GetUserIdWithDomain();
        var myPrincipal = new WindowsPrincipal(myIdentity);
        return myPrincipal.IsInRole(groupName);
    }
    
    
    public bool IsInGroup(List<string> groupNames)
    {
        var myIdentity = GetUserIdWithDomain();
        var myPrincipal = new WindowsPrincipal(myIdentity);
    
        if (groupNames.Any(@group => myPrincipal.IsInRole(@group) == true))
        {
            return true;
        }
        else
        {
            return false;
        }
    
    }
    
    public static WindowsIdentity GetUserIdWithDomain()
    {
        WindowsIdentity myIdentity = WindowsIdentity.GetCurrent();
        return myIdentity;
    }
    
    public static string GetUserId()
    {
        var id = GetUserIdWithDomain().Name.ToString().Split("\\");
        return id(1);
    }
    
    
    public static string GetUserDisplayName()
    {
        var id = GetUserIdWithDomain().Name.ToString().Split("\\");
    
        var dc = new PrincipalContext(ContextType.Domain, id(0));
        var adUser = UserPrincipal.FindByIdentity(dc, id(1));
        return adUser.DisplayName;
    
    }
    
    
    }
    

    编辑 1

    如果您的 cmets 是用户返回,您需要将以下内容添加到您的 web.config 文件中

      <system.web>
        <identity impersonate="true" />
       ...
      </system.web>
    

    然后在 IIS 中转到您的站点\应用程序,然后转到授权,然后禁用匿名用户,启用 ASP.Net 模拟和 Windows 身份验证

    编辑 2

    当您被挑战访问权限时,这意味着您没有设置正确的权限

    【讨论】:

    • 我在 Page_Load 事件中写了以下行 'Response.Write("Username: "+ WindowsIdentity.GetCurrent().Name);'并在 IIS 中托管网站。我在页面中收到以下消息:用户名:IIS APPPOOL\TestWeb 相反,它应该显示我的域名和网络 ID,例如用户名:mydomain\mynetowrkid
    • 好的,这给了你什么?另外,您的 IIS 设置如何重新验证您网站上的用户。
    • 在此之后获得 Windows 身份验证我在 IIS Anonymous Authentication Disabled ASP.NET Impersonation Disabled Forms Authentication Disabled Windows Authentication Enabled 和 web.config 中做了以下操作 现在如果我浏览我弹出 Windows 安全性的网站并询问我的 Windows 凭据。我尝试使用我的 Windows 登录详细信息,但出现以下消息:用户名:mydomain\mynetworkid 密码:我的 Windows 密码 HTTP 错误 401.1 - 未经授权
    • 您的 Web 应用程序在服务器上的什么位置?您的用户是否有对该文件夹\区域的权限?
    • @Sukhi 只是再次阅读您的 cmets,您需要启用模拟
    猜你喜欢
    • 2010-11-28
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    相关资源
    最近更新 更多