在c#中获取当前用户的FQDN

时间:2016-07-13 07:18:08

标签: c# .net active-directory windows-authentication

我遇到了一个显然很简单的问题,但这给我带来了一些麻烦。

假设我有两个可信赖的AD域:  的 DEMO.intern DEMO.trust

我需要在我的应用中使用Windows身份验证获取当前用户的FQDN。两个域中的用户都可以使用它。我需要知道哪个用户来自哪个域。

显然,

import sys, threading
sys.setrecursionlimit(10**6) # max depth of recursion
threading.stack_size(2**27)  # new thread will get stack of such size


class Node:
    def __init__(self, val):
        self.l = None
        self.r = None
        self.v = val

class Tree:
    def __init__(self):
        self.root = None

    def getRoot(self):
        return self.root

    def add_root(self, val):
        if(self.root is None):
            self.root = Node(val)

    def add_left(self, val, node):
        if(node.l is None):
            node.l = Node(val)

    def add_right(self, val, node):
        if(node.r is None):
            node.r = Node(val)

    def deleteTree(self):
        # garbage collector will do this for us. 
        self.root = None


    def inOrder(self):
        self.result = []
        if(self.root is not None):
            self._inOrder(self.root, self.result)
            return self.result
        else:
            print('root is None')

    def _inOrder(self, node, result):
        if(node != None):
            self._inOrder(node.l, self.result)
            self.result.append(node.v)
            self._inOrder(node.r, self.result)

    def read(self):
        self.n = int(sys.stdin.readline())
        self.key = [0 for i in range(self.n)]
        self.left = [0 for i in range(self.n)]
        self.right = [0 for i in range(self.n)]
        for i in range(self.n):
            [a, b, c] = map(int, sys.stdin.readline().split())
            self.key[i] = a
            self.left[i] = b
            self.right[i] = c

        #adding root
        self.add_root(self.key[0])
        if self.left[0] != -1: 
            #add left of root
            self.add_left(self.key[self.left[0]], self.root)
        if self.right[0] != -1:
            #add right of root
            self.add_right(self.key[self.right[0]], self.root)

        #where it is not adding left and right nodes
        for i in range(1, self.n):
            if self.left[i] != -1:
                # adding the other left nodes
                self.add_left(self.key[self.left[i]], Node(self.key[i]))
            if self.right[i] != -1:
                # adding the other right nodes
                self.add_right(self.key[self.right[i]], Node(self.key[i]))

def main():
    tree = Tree()
    tree.read()
    print(" ".join(str(x) for x in tree.inOrder()))
    #print(" ".join(str(x) for x in tree.preOrder()))
    #print(" ".join(str(x) for x in tree.postOrder()))

threading.Thread(target=main).start()

Environment.UserDomainName 

在两种情况下都无法返回 DEMO

我知道:

 
System.Security.Principal.WindowsIdentity.GetCurrent().Name

为我提供了本地计算机的完整域名,但如何为当前登录的用户实现这一目标?

我也尝试过:

System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName

没有成功给我错误"没有这样的主人知道"。那么如何获取登录用户的完整域名,例如 DEMO.intern ,而不仅仅是 DEMO

1 个答案:

答案 0 :(得分:1)

经过一番研究,这可以完成工作:

public static class GetUserNameExUtil
{
    #region Interop Definitions
    public enum EXTENDED_NAME_FORMAT 
    {
        NameUnknown = 0,
        NameFullyQualifiedDN = 1,
        NameSamCompatible = 2,
        NameDisplay = 3,
        NameUniqueId = 6,
        NameCanonical = 7,
        NameUserPrincipal = 8,
        NameCanonicalEx = 9,
        NameServicePrincipal = 10,
        NameDnsDomain = 12,
    }
    [System.Runtime.InteropServices.DllImport("secur32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    public static extern int GetUserNameEx(int nameFormat, StringBuilder userName, ref int userNameSize);
    #endregion

    public static string GetUserName(EXTENDED_NAME_FORMAT nameFormat)
    {
        if (Environment.OSVersion.Platform != PlatformID.Win32NT)
        {
            return null;
        }

        StringBuilder userName = new StringBuilder(1024);
        int userNameSize = userName.Capacity;
        if (GetUserNameEx((int)nameFormat, userName, ref userNameSize) != 0)
        {
            string[] nameParts = userName.ToString().Split('\\');
            return nameParts[0];
        }

        return null;
    }
    public static string GetUserFullName()
    {
        return GetUserName(EXTENDED_NAME_FORMAT.NameDnsDomain);
    }
}