2016-04-29 3 views
14

WCFサービスへ通過する取得DefaultNetworkCredentialsは私がweb.configファイルに次のような構成でのWebApplicationに作成したWCFサービスを持っている

このサービスは、情報を見るにはWindowsCredentialsに取る必要が
<service name="RedwebServerManager.WebApplication.DesktopService" 
      behaviorConfiguration="ServBehave"> 
    <endpoint 
     address="" 
     binding="basicHttpBinding" 
     bindingConfiguration="basicBind" 
     contract="RedwebServerManager.WebApplication.IDesktopService"/> 
    </service> 
</services> 
<bindings> 
    <basicHttpBinding> 
    <binding name="basicBind"> 
     <security mode="TransportWithMessageCredential"> 
     <transport clientCredentialType="Windows"/> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</bindings> 

認証されたユーザに基づくデータベース。このサービス電流は、次のシグネチャを持つインタフェースを実装する一つの方法を持っている

[ServiceContract] 
public interface IDesktopService 
{ 
    /// <summary> 
    /// Gets the clients. 
    /// </summary> 
    /// <returns>IEnumerable&lt;ClientServiceModel&gt;.</returns> 
    [OperationContract] 
    IEnumerable<ClientServiceModel> GetClients(); 
} 

私は、Windowsは、WCFサービスを消費しているフォームアプリケーションがあると私は、この意志などのアプリケーションを使用して、現在のユーザーの資格情報を通過したいですすべて私たちのドメインに座っている。

<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
      <binding name="BasicHttpBinding_IDesktopService"> 
       <security mode="TransportWithMessageCredential"> 
       <transport clientCredentialType="Windows" proxyCredentialType="Windows"/> 
       </security> 
      </binding> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="https://redwebservermanager.redweb.network/DesktopService.svc" 
      binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDesktopService" 
      contract="ManagerService.IDesktopService" name="BasicHttpBinding_IDesktopService" /> 
    </client> 
</system.serviceModel> 

を次のように私は手動で資格情報がユーザ名と私は以上の現在のユーザーの資格情報を渡すことができるように、私は

managerService.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials 

をしようとしているが、パスワードのすべてが正常に動作しますが、私に設定した場合はapp.configですGetClients()への呼び出しでユーザー名とパスワードが設定されていないというエラーが発生し続ける。

誰でも手伝ってもらえますか?私はまた、方法に追加しようとしました

[OperationBehavior(Impersonation = ImpersonationOption.Required)] 

しかし、これは違いがありませんでした。

+0

私はそれがばかげているのは分かっていますが、** ClientCredentialは設定していません。私たちは 'NetTcpBinding'をトランスポートセキュリティと共に使用しています。 –

答えて

6

あなたは、次のいずれかの方法を使用してユーザー名(およびパスワード以外のより多くの情報)を取得することができます:

//1. 
System.Security.Principal.WindowsIdentity.GetCurrent(); 
//2.  
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); 
    WindowsPrincipal user = (WindowsPrincipal)System.Threading.Thread.CurrentPrincipal; 
//3.  
WindowsIdentity ident = WindowsIdentity.GetCurrent(); 
    WindowsPrincipal user = new WindowsPrincipal(ident); 

しかし、ユーザーのパスワードにアクセスできる方法はありません。あなたはアイデンティティ情報を受け取ったら、このquestion

を参照してください、あなたは同様に、OutgoingMessageHeadersを使用してWCFにユーザー名を渡すことができます

MessageHeader<string> header = new MessageHeader<string>(userName); 
     OperationContextScope contextScope = new OperationContextScope(InnerChannel); 
     OperationContext.Current.OutgoingMessageHeaders.Add(
     header.GetUntypedHeader("String", "System")); 

そして、WCFサービスの実装ではあなたはそれが好きで読むことができます:

OperationContext context = OperationContext.Current; 

      if (context != null) 
      { 
       try 
       { 
        _LoginName = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>("String", "System");   
       } 
       catch 
       { 
        _LoginName = string.Empty; 
       } 
      } 

これが役立つか他にご質問がありましたらお知らせください。

ありがとう、 Soma。

関連する問題