2009-06-01 4 views

답변

3

은 내가 당신의 질문을 이해 모르겠지만, 당신은 서비스 작업에 대한 호출을 Windows 사용자의 도메인 이름을 필요로하는 경우,이 사용

OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name 

이 돌아갑니다 "{도메인 }\{사용자 이름}".

이 시도하고 저를 당신이 (당신은 아마 MSTEST 프로젝트에이 코드를 붙여 넣을 것) 무엇을 생각하는지에 관해 알려주세요 :

[TestClass] 
public class AlternativeCredentials 
{ 
    // Contracts 
    [ServiceContract] 
    interface IMyContract 
    { 
     [OperationContract] 
     string GetUserName(); 
    } 

    // Service 
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)] 
    class MyService : IMyContract 
    { 
     public string GetUserName() 
     { 
      return OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name; 
     } 
    } 

    // Client 
    class MyContractClient : ClientBase<IMyContract>, IMyContract 
    { 
     public MyContractClient() { } 
     public MyContractClient(Binding binding, string address) : 
      base(binding, new EndpointAddress(address)) { } 

     public string GetUserName() 
     { return Channel.GetUserName(); } 
    } 

    #region Host 
    static string address = "net.tcp://localhost:8001/" + Guid.NewGuid().ToString(); 
    static ServiceHost host; 

    [ClassInitialize()] 
    public static void MyClassInitialize(TestContext testContext) 
    { 
     host = new ServiceHost(typeof(MyService)); 
     host.AddServiceEndpoint(typeof(IMyContract), new NetTcpBinding(), address); 
     host.Open(); 
    } 

    [ClassCleanup()] 
    public static void MyClassCleanup() 
    { 
     if (host.State == CommunicationState.Opened) 
      host.Close(); 
    } 
    #endregion 

    [TestMethod] 
    public void UseUserNameCredentials() 
    { 
     using (MyContractClient proxy = 
      new MyContractClient(new NetTcpBinding(), address)) 
     { 
      proxy.ClientCredentials.UserName.UserName = "MyUsername"; 
      proxy.ClientCredentials.UserName.Password = "MyPassword"; 

      proxy.Open(); 
      Assert.AreEqual("EMS\\magood", proxy.GetUserName()); 
      proxy.Close(); 
     } 
    } 
} 
+0

나는 이것을 시도 할 것이다. 고마워요. – DDiVita

+0

잠시 생각한 후에, 나는 실제적인 서비스에 대한 어떠한 보안도하지 않고 있습니다. 이 메서드를 호출하면 프로세스를 실행하는 사용자가 어떤 사용자인지 알 수 있습니까? 나는 그것이 서비스를 실행하는 시스템 계정의 도메인/사용자 이름을 보여줄 것이라고 생각한다. 권리? – DDiVita

+0

위의 예에서 "EMS \ magood"는 내 Windows ID입니다. WCF의 기본값은 ClientCredentialType = Windows입니다. TCP 바인딩 용 기본 설정으로 변경할 수 있습니다. 어떤 종류의 바인딩을 사용하고 있습니까? –

관련 문제