外部请求的身份验证 - 如何在SOAP标头中传递用户凭据?

时间:2016-04-18 14:18:33

标签: c# web-services wcf soap wsdl

有一个简单的WCF Web服务。必须对Web服务的所有外部请求进行身份验证。

网络服务合同:

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

Web服务的实现:

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

通过使用wsdl.exe实用程序,我生成代理类来调用Web服务的方法。

生成的代码如下所示:

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]));
        }
    }
}

我发现了这个讨论,我想,这就是我的需要:

我想在邮件的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"

我应该将丢失的代码包含在生成的代理类中吗?

我将非常感谢这些信息。谢谢大家。

1 个答案:

答案 0 :(得分:5)

您应该从凭据缓存中传递凭证,如下所示:

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

或明确说明:

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

我也喜欢使用client.PreAuthenticate = true;,但可能存在问题,我忽略了。

相关问题