从机器上下文中获取用户全名
我有一个在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原问题中的函数需要修改。如果您尝试访问返回的 UserPrincipal 对象,您将收到 ObjectDisposeException
此外,User.Identity.Name 不可用,需要传入。
我对上面的函数进行了以下更改。
此外,我需要使用的 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.
Furthermore, the property of UserPrincipal I need to use is DisplayName (instead of GivenName and Surname);