2012-06-21 3 views
7

웹 응용 프로그램 localhost를 실행할 때 아래 코드가 제대로 작동하지만 IIS 서버에 설치하지 않는 이유는 무엇입니까? 이 Active Directory에 추가 호출없이 SID 또는 UPN에 대한 액세스를 제공하지 않는 한System.DirectoryServices.AccountManagement.UserPrincipal - localhost (iis가 아님)

using (HostingEnvironment.Impersonate()) 
{ 
    UserPrincipal activeUser = UserPrincipal.Current; 
    String activeUserSid = activeUser.Sid.ToString(); 
    String activeUserUPN = activeUser.UserPrincipalName; 
} 

내가 HttpContext.Current.User 스틱 제안하지 마십시오.

웹 응용 프로그램은 세 개의 별도 도메인에서 Windows 인증을받은 사용자가 사용하며 웹 서버는 네 번째 도메인에서 호스팅됩니다. 응용 프로그램 풀이 NetworkService ID로 실행되도록 구성되었으며 웹 응용 프로그램 구성에 ID 가장이 true로 설정되어 있습니다. 이 IIS에서 실행

오류 메시지는 다음과 같습니다

Error in Page_Load(): UserPrincipal.Current.
System.InvalidCastException: Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'.
at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue)
at System.DirectoryServices.AccountManagement.UserPrincipal.get_Current()
at webapp.Details.Default.Page_Load(Object sender, EventArgs e)

편집 : 시도 다음 두 불행하게도 같은 오류가 발생합니다.

UserPrincipal userPrincipal = UserPrincipal.Current; 
Response.Write(userPrincipal.Name); 
Principal userOrGroup = UserPrincipal.Current; 
Response.Write(userOrGroup.Name); 

답변

1

그것은 필요와 같은 사용자를 결정하는 다른 방법을 보인다.
여기서 msdn 속성 설명 :
"스레드가 실행되는 현재 사용자를 나타내는 사용자 기본 개체를 가져옵니다."
따라서 UserPrincipal.Current는 실행중인 IIS 하에서 사용자를 반환합니다.

+0

나는 당신이 옳다고 생각합니다. 이것은 작동하지 않습니다 ... var wi = HttpContext.Current.User.Identity as WindowsIdentity; using (wi.Impersonate()) { UserPrincipal up = UserPrincipal.Current; activeUserUPN = up.UserPrincipalName; activeUserSid = up.Sid.ToString(); } – RichardD

+1

IIS에서 "스레드가 실행중인 현재 사용자"는 IIS 응용 프로그램 풀 ID 일 가능성이 큽니다. 그러나 Visual Studio, Casini 또는 IIS Express에서 PC를 실행할 때 실행됩니다. 이 경우 사용자 ID는 페이지 및 로컬로 실행중인 개발 서버를 요청하는 클라이언트에 의해 공유됩니다. –

3

저는 UserPrincipal.Current를 배포하는 데 많은 어려움을 겪었지만 여전히 그 이유를 충분히 이해하지 못했습니다.

결국 PrincipalSearcher를 사용하여 결국 UserPrincipal.Current가 수행 중이라고 생각했던 작업을 수행하기 위해 다음 함수를 만들었습니다.

private UserPrincipal GetActiveDirectoryUser(string userName) 
{ 
    using(var ctx = new PrincipalContext(ContextType.Domain)) 
    using(var user = new UserPrincipal(ctx) { SamAccountName = userName}) 
    using(var searcher = new PrincipalSearcher(user)) 
    { 
     return searcher.FindOne() as UserPrincipal; 
    } 
} 

는 그리고는 이름으로 그 방법에 System.Web.HttpContext.Current.User.Identity.Name을 통과시켰다.

+0

이 오류가 발생합니다 :'System.ObjectDisposedException : 폐기 된 객체에 접근 할 수 없습니다. 개체 이름 : 'PrincipalContext'. System.DirectoryServices.AccountManagement.PrincipalContext.CheckDisposed()에서 System.DirectoryServices.AccountManagement.PrincipalContext.get_ContextType() System.DirectoryServices.AccountManagement.Principal.get_Name()에서 System.String.Concat (Object [] args)에서 Admin_Click (Object sender, EventArgs e)' – SearchForKnowledge

+1

MVC 응용 프로그램에서 여러 가지를 시도했습니다. 'DirectoryEntry de = new DirectoryEntry ("WinNT : //"+ 환경.UserDomainName + "/"+ userId); 문자열 자명 de.Properties = [ "fullName의"] Value.ToString().이어서 '난'System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName 시도;' 그런 다음 용액을했는데, 그것은이었다 내 프로덕션 서버로 배포 할 때 하나만 작동합니다. 이유는 확실하지 않습니다. 모두 로컬로 작업 ... –

관련 문제