컴퓨터 컨텍스트에서 사용자의 이름을 얻습니다
-
19-09-2019 - |
문제
인트라넷에서 실행되는 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 == 머신 일 때 user -prinicipal이 검색 될 때 Givenname 또는 성과 같은 속성을 얻지 못한다는 것입니다. 사용자 (Windows Server 2008)를 만들 때 이러한 값을 설정하는 방법이 있습니까? 아니면 다른 방식으로이를 수행해야합니까?
해결책
원래 질문의 기능을 수정해야합니다. 리턴 된 userprincipal 객체에 액세스하려고하면 ObjectDispossionException이 나타납니다.
또한 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 (givenname 및 surname 대신)입니다.
제휴하지 않습니다 StackOverflow