2016-04-18 23 views
1

単純なWCF Webサービスがあります。 Webサービスへの外部リクエストはすべて認証されている必要があります。外部リクエストの認証 - SOAPヘッダーにユーザーの資格情報を渡す方法?

Webベースのサービスの契約:

namespace SimpleCustomService 
{ 
    [ServiceContract] 
    public interface UsrIService 
    { 
     [OperationContract] 
     string SayHello(); 
    } 
} 

Webベースのサービスの実装:私は、Webサービスのメソッドを呼び出すためのプロキシクラスを生成wsdl.exeユーティリティを使用することにより

namespace SimpleCustomService 
{ 
    public class UsrService : UsrIService 
    { 
     public string SayHello() 
     { 
      return "Hello"; 
     } 
    } 
} 

。次のように

生成されたコードが見えます:

using System; 
using System.ComponentModel; 
using System.Diagnostics; 
using System.Web.Services; 
using System.Web.Services.Protocols; 
using System.Xml.Serialization; 

// 
// This source code was auto-generated by wsdl, Version=4.6.1055.0. 
// 


/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.6.1055.0")] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Web.Services.WebServiceBindingAttribute(Name="BasicHttpBinding_UsrIService", Namespace="http://tempuri.org/")] 
public partial class UsrService : System.Web.Services.Protocols.SoapHttpClientProtocol { 

    private System.Threading.SendOrPostCallback SayHelloOperationCompleted; 

    /// <remarks/> 
    public UsrService() { 
     this.Url = "http://localhost:8080/0/ServiceModel/SimpleCustomService.svc/soap"; 
    } 

    /// <remarks/> 
    public event SayHelloCompletedEventHandler SayHelloCompleted; 

    /// <remarks/> 
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/UsrIService/SayHello", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] 
    [return: System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] 
    public string SayHello() { 
     object[] results = this.Invoke("SayHello", new object[0]); 
     return ((string)(results[0])); 
    } 

    /// <remarks/> 
    public System.IAsyncResult BeginSayHello(System.AsyncCallback callback, object asyncState) { 
     return this.BeginInvoke("SayHello", new object[0], callback, asyncState); 
    } 

    /// <remarks/> 
    public string EndSayHello(System.IAsyncResult asyncResult) { 
     object[] results = this.EndInvoke(asyncResult); 
     return ((string)(results[0])); 
    } 

    /// <remarks/> 
    public void SayHelloAsync() { 
     this.SayHelloAsync(null); 
    } 

    /// <remarks/> 
    public void SayHelloAsync(object userState) { 
     if ((this.SayHelloOperationCompleted == null)) { 
      this.SayHelloOperationCompleted = new System.Threading.SendOrPostCallback(this.OnSayHelloOperationCompleted); 
     } 
     this.InvokeAsync("SayHello", new object[0], this.SayHelloOperationCompleted, userState); 
    } 

    private void OnSayHelloOperationCompleted(object arg) { 
     if ((this.SayHelloCompleted != null)) { 
      System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg)); 
      this.SayHelloCompleted(this, new SayHelloCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState)); 
     } 
    } 

    /// <remarks/> 
    public new void CancelAsync(object userState) { 
     base.CancelAsync(userState); 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.6.1055.0")] 
public delegate void SayHelloCompletedEventHandler(object sender, SayHelloCompletedEventArgs e); 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.6.1055.0")] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
public partial class SayHelloCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs { 

    private object[] results; 

    internal SayHelloCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
      base(exception, cancelled, userState) { 
     this.results = results; 
    } 

    /// <remarks/> 
    public string Result { 
     get { 
      this.RaiseExceptionIfNecessary(); 
      return ((string)(this.results[0])); 
     } 
    } 
} 

私はそれが私の場合には必要なものですが、私が思うに、この議論を発見し、:

IメッセージのSOAPヘッダーに資格情報を渡したいどうやってするの?

私はこのコードを使用する場合:

using (UsrService client = new UsrService()) 
{ 
    client.ClientCredentials.UserName.UserName = "user"; 
    client.ClientCredentials.UserName.Password = "password"; 
    client.SayHello(); 
} 

を私は、このエラーメッセージが表示されます:

"UsrService does not contain a difinition for ClientCredentials" 

私は、生成されたプロキシクラスに不足しているコードを含めるべきか?

私は情報に非常に感謝します。ありがとうございます。

答えて

2

あなたがそうのような資格キャッシュからそれに資格を渡す必要があります。

client.Credentials = System.Net.CredentialCache.DefaultCredentials; 

または明示的にそれを与える:

client.Credentials = new System.Net.NetworkCredential("username", "password"); 

また、私はclient.PreAuthenticate = true;を使用したいが、その周りに問題があるかもしれません私は無視している。

+0

お返事ありがとうございました!今私はそれをやろうとします。 –

+0

ありがとうございました! –

+0

それはうれしかった@AlekseyBykov。 –

関連する問題