从机器上下文中获取用户全名

发布于 2024-08-06 00:52:47 字数 1006 浏览 9 评论 0原文

我有一个在 Intranet 上运行的 ASP.NET 应用程序。在生产中,我可以从域上下文中获取用户,并可以访问大量信息,包括他们的名字和姓氏(UserPrincipal.GivenName 和 UserPrincipal.Surname)。

我们的测试环境不属于生产域,测试用户在测试环境中没有域帐户。因此,我们将他们添加为本地计算机用户。当他们浏览到起始页面时,系统会提示他们输入凭据。我使用以下方法来获取 UserPrincipal

public static UserPrincipal GetCurrentUser()
        {
            UserPrincipal up = null;

            using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
            {
                up = UserPrincipal.FindByIdentity(context, User.Identity.Name);
            }

            if (up == null)
            {
                using (PrincipalContext context = new PrincipalContext(ContextType.Machine))
                {
                    up = UserPrincipal.FindByIdentity(context, User.Identity.Name);
                }
            }

            return up;
        }

我这里遇到的问题是,当 ContextType == Machine 检索 UserPrincipal 时,我没有获取诸如 GiveName 或 Surname 之类的属性。有没有办法在创建用户(Windows Server 2008)时设置这些值,或者我是否需要以不同的方式进行设置?

I have an ASP.NET application that runs on our intranet. In production I can get the user from the domain context and have access to lots of information including their first and last name (UserPrincipal.GivenName and UserPrincipal.Surname).

Our test environment is not part of the production domain and test users do not have domain accounts in the test environment. So, we add them as local machine users. They are prompted for credentials when they browse to the start page. I use the following method to get the UserPrincipal

public static UserPrincipal GetCurrentUser()
        {
            UserPrincipal up = null;

            using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
            {
                up = UserPrincipal.FindByIdentity(context, User.Identity.Name);
            }

            if (up == null)
            {
                using (PrincipalContext context = new PrincipalContext(ContextType.Machine))
                {
                    up = UserPrincipal.FindByIdentity(context, User.Identity.Name);
                }
            }

            return up;
        }

The problem I have here is that when the UserPrinicipal is retrived when ContextType == Machine I do not get properties like GivenName or Surname. Is there a way to set these values when creating the user (Windows Server 2008) or do I need to go about this in a different way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如歌彻婉言 2024-08-13 00:52:47

原问题中的函数需要修改。如果您尝试访问返回的 UserPrincipal 对象,您将收到 ObjectDisposeException

此外,User.Identity.Name 不可用,需要传入。

我对上面的函数进行了以下更改。

public static UserPrincipal GetUserPrincipal(String userName)
        {
            UserPrincipal up = null;

            PrincipalContext context = new PrincipalContext(ContextType.Domain);
            up = UserPrincipal.FindByIdentity(context, userName);

            if (up == null)
            {
                context = new PrincipalContext(ContextType.Machine);
                up = UserPrincipal.FindByIdentity(context, userName);
            }

            if(up == null)
                throw new Exception("Unable to get user from Domain or Machine context.");

            return up;
        }

此外,我需要使用的 UserPrincipal 的属性是 DisplayName (而不是 GiveName 和 Surname);

The function in the original question needs to be modified. If you try to access the UserPrincipal object that is returned, you will get an ObjectDisposedException

Also, the User.Identity.Name is not available and needs to be passed in.

I have made the following changes to the function above.

public static UserPrincipal GetUserPrincipal(String userName)
        {
            UserPrincipal up = null;

            PrincipalContext context = new PrincipalContext(ContextType.Domain);
            up = UserPrincipal.FindByIdentity(context, userName);

            if (up == null)
            {
                context = new PrincipalContext(ContextType.Machine);
                up = UserPrincipal.FindByIdentity(context, userName);
            }

            if(up == null)
                throw new Exception("Unable to get user from Domain or Machine context.");

            return up;
        }

Furthermore, the property of UserPrincipal I need to use is DisplayName (instead of GivenName and Surname);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文