65.9K
CodeProject 正在变化。 阅读更多。
Home

Web 服务的身份验证(使用 SOAP 头)

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.63/5 (57投票s)

2003年6月25日

2分钟阅读

viewsIcon

890335

downloadIcon

14746

使用 SOAP 标头进行 Web 服务简单身份验证。

Sample Image - AuthForWebServices.gif

引言

我最近为一位客户开发了一些 Web 服务,这些服务返回了一些敏感数据。我需要找到一种简单的方法来验证这些 Web 服务的用户身份。这就是我采取的方法。

背景

我最近开始在开发的应用程序中比较频繁地使用 Web 服务,在大多数情况下,它们传递的信息适合公开领域。然而,最近的一个项目迫使我研究不同的身份验证方法。

我的要求是,客户端应用程序的身份验证必须简单,并且必须使用基于 Web 的管理系统。这阻止我使用 Windows 身份验证(对于此 Web 服务的客户端来说,使用起来非常简单)。通过使用 SOAP 标头传递用户名和密码信息,可以大大简化任何身份验证请求。

使用代码

我希望让客户端能够很容易地理解。

protected System.Web.UI.WebControls.DataGrid dgData;
    
private void Page_Load(object sender, System.EventArgs e)
{
    //simple client

    AuthWebService.WebService webService = new AuthWebService.WebService();
    AuthWebService.AuthHeader authentication = new 
                              AuthWebService.AuthHeader();

    authentication.Username = "test";
    authentication.Password = "test";
    webService.AuthHeaderValue = authentication;

    //Bind the results - do something here

    DataSet dsData = webService.SensitiveData();

    dgData.DataSource = dsData;
    dgData.DataBind();    

}

基本上,客户端需要做的就是创建一个身份验证对象,填写用户名和密码,然后将它们传递给 Web 服务对象。Web 服务代码也很简单,.NET 框架允许您通过从 SoapHeader 类派生来创建自定义 SOAP 标头,因此我们希望添加用户名和密码。

using System.Web.Services.Protocols;

public class AuthHeader : SoapHeader
{
    public string Username;
    public string Password;
}

下一步是确定需要身份验证的 Web 服务,在包含的示例中,它是方法 SensitiveData。为了强制使用我们新的 SOAP 标头,我们需要将以下属性添加到我们的方法中

[SoapHeader ("Authentication", Required=true)]

因此,我们 Web 服务方法的完整定义是

public AuthHeader Authentication;


[SoapHeader ("Authentication", Required=true)]
[WebMethod (Description="Returns some sample data")]
public DataSet SensitiveData()
{
    DataSet data = new DataSet();
            
    //Do our authentication

    //this can be via a database or whatever

    if(Authentication.Username == "test" && 
                Authentication.Password == "test")
    {
        //they are allowed access to our sensitive data

        
        //just create some dummy data

        DataTable dtTable1 = new DataTable();
        DataColumn drCol1 = new DataColumn("Data", 
                System.Type.GetType("System.String"));
        dtTable1.Columns.Add(drCol1);

        DataRow drRow = dtTable1.NewRow();
        drRow["Data"] = "Sensitive Data";
        dtTable1.Rows.Add(drRow);
        dtTable1.AcceptChanges();

        data.Tables.Add(dtTable1);
    
    }else{
        data = null;
    }            

    return data;
}

我还应该提到,当我谈论 SOAP 标头时,我实际上指的是 SOAP 请求中的 soap:Header 元素,它与随请求发送的 HTTP 标头无关。SOAP 请求看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <AUTHHEADER xmlns="http://tempuri.org/">
      <USERNAME>string</USERNAME>
      <PASSWORD>string</PASSWORD>
    </AUTHHEADER>
  </soap:Header>
  <soap:Body>
    <SENSITIVEDATA xmlns="http://tempuri.org/" />
  </soap:Body>
</soap:Envelope>

我已在附件中包含客户端和 Web 服务。

历史

  • 2003/06/25 - 创建文章
© . All rights reserved.