ÔÚµ÷ÓÃWeb Serivicesʱ,ÍùÍùÐèÒªÉí·ÝÑéÖ¤,ʹµÃͨ¹ýÑéÖ¤µÄÓû§²ÅÄܵ÷ÓÃÄãWeb SerivicesÖеķ½·¨.µ±È»Äã¿ÉÒÔͨ¹ý½«²ÎÊýÌí¼Óµ½Ã¿¸öÐèÒª×Ô¶¨ÒåÉí·ÝÑéÖ¤·½°¸µÄWeb services·½·¨ÖÐÈ¥,ÕâÐèÒª»¨·ÑºÜ´óµÄ¾«Á¦.IssueVision ÖÐʹÓÃÁ˷dz£³£ÓöøÇÒÓÐЧ±ã½ÝµÄ·½·¨-----ʹÓÃSoapHeaderÀ´ÊµÏÖ×Ô¶¨ÒåÉí·ÝÑéÖ¤Êý¾ÝµÄ´«µÝ.
SoapHeaderÌṩÁËÒ»ÖÖ·½·¨£¬ÓÃÓÚ½«Êý¾Ý´«µÝµ½Web services·½·¨»ò´ÓWeb services·½·¨´«µÝÊý¾Ý£¬Ìõ¼þÊǸÃÊý¾Ý²»Ö±½ÓÓëWeb services ·½·¨µÄÖ÷¹¦ÄÜÏà¹Ø. Äã²»Óý«²ÎÊýÌí¼Óµ½Ã¿¸öÐèÒª×Ô¶¨ÒåÉí·ÝÑéÖ¤·½°¸µÄWeb services ·½·¨£¬¶ø¿ÉÒÔ½«ÒýÓÃ´Ó SoapHeader ÅÉÉúµÄÀàµÄ SoapHeaderAttribute Ó¦ÓÃÓÚÿ¸öWeb services ·½·¨¡£´Ó SoapHeader ÅÉÉúµÄÀàµÄʵÏÖ´¦Àí¸Ã×Ô¶¨ÒåÉí·ÝÑéÖ¤·½°¸. IssueVision ¾ÍÊÇÀûÓÃSoapHeaderµÄÕâÖÖÄÜÁ¦À´ÊµÏÖ×Ô¶¨ÒåÉí·ÝÑéÖ¤Êý¾Ý´«µÝµÄ.
ÎÒÃÇÀ´¿´Ò»ÏÂÈçºÎÀûÓÃSoapHeaderÀ´´«µÝÊý¾Ý.
1. Ê×ÏÈÐèÒªÔÚ·þÎñÖж¨ÒåÒ»¸ö´Ó SOAPHeader ÅÉÉúµÄÀà,±íʾ´«Èë SOAP ±êÍ·µÄÊý¾Ý.
IssueVision ÔÚÖÐIssueVisionWebÏîÄ¿(´ËÏîÄ¿ÓÃÓÚ·¢²¼Web Services)ÖÐͨ¹ý´´½¨CredentialSoapHeaderÀàÀ´ÊµÏÖµÚÒ»²½.
CredentialSoapHeader.cs
using System.Web.Services.Protocols;
namespace IssueVision.Web
{
public class CredentialSoapHeader : SoapHeader
{
private string m_username;
private string m_password;
public string Username
{
get{ return m_username;}
set{ m_username = value;}
}
public string Password
{
get{ return m_password;}
set{ m_password = value;}
}
}
}
2. ½«·þÎñµÄ¹«¹²×Ö¶ÎÉùÃ÷Ϊ¸ÃÀàÐÍ,ʹ¸ÃSoapHeaderÔÚWeb ServicesµÄ¹«¹²ºÏͬÖй«¿ª,²¢ÔÚ´´½¨´úÀíʱ¿ÉÓɿͻ§¶ËʹÓÃ.
IssueVisionµÄWeb Services----IssueVisionServices.asmxÈç´ËʵÏÖ.
IssueVisionServices.asmx´úÂëÆ¬¶Ï:
public class IssueVisionServices : WebService
{
...
private CredentialSoapHeader m_credentials;
// custom SOAP header to pass credentials
public CredentialSoapHeader Credentials
{
get { return m_credentials; }
set { m_credentials = value; }
}
.......
}
3. ÔÚWeb ServicesʹÓà SoapHeader ×Ô¶¨ÒåÊôÐÔ¶¨ÒåÒ»×鹨ÁªµÄ±êÍ·,·þÎñÖеÄÿ¸ö WebMethod ¶¼¿ÉÒÔʹÓÃ.(ĬÈÏÇé¿öÏ£¬±êÍ·ÊDZØÐèµÄ£¬µ«Ò²¿ÉÒÔ¶¨Òå¿ÉÑ¡±êÍ·)
IssueVisionServices.asmx´úÂëÆ¬¶Ï:
....
[WebMethod(Description="Returns the lookup tables for IssueVision.")]
[SoapHeader("Credentials")]
public IVDataSet GetLookupTables()
{
SecurityHelper.VerifyCredentials(this);
return new IVData().GetLookupTables();
}
SecurityHelperÀàµÄVerifyCredentials·½·¨ÓÃÀ´´ÓWeb ServicesÖеÄSoapHeaderÀàÀ´µÃµ½×Ô¶¨ÒåÉí·ÝÑé֤ƾ¾Ý(ÈçÓû§ÃûºÍÃÜÂë).
SecurityHelper.cs´úÂëÆ¬¶ÏÈçÏÂ:
// verifies the clients credentials
public static void VerifyCredentials(IssueVisionServices service)
{
if (service.Credentials == null || service.Credentials.Username == null || service.Credentials.Password == null ) //Èç¹ûûÓÐÈÏÖ¤ÐÅÏ¢,·µ»ØSoapException,ÕâÑù¾Í²»ÄÜÄäÃûµ÷ÓÃWeb MethodÁË
{
EventLogHelper.LogFailureAudit("A login was attempted with missing credential information.");
throw new SoapException(string.Empty, SoapException.ClientFaultCode, "Security");
}
string password = Authenticate(service.Credentials);
}
// authenticates a user's credentials passed in a custom SOAP header
private static string Authenticate( CredentialSoapHeader header)
{
DataSet dataSet = new DataSet();
string dbPasswordHash;
try
{
SqlConnection conn = new SqlConnection(Common.ConnectionString);
SqlCommand cmd = new SqlCommand("GetUser", conn);
cmd.Parameters.Add("@UserName", header.Username);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dataSet);
}
catch (Exception ex)
{
EventLogHelper.LogFailureAudit(string.Format("The GetUser stored procedure encounted a problem: {0}", ex.ToString()));
throw new SoapException(string.Empty, SoapException.ServerFaultCode, "Database");
}
// does the user exist?
if (dataSet.Tables[0].Rows.Count == 0)
{
EventLogHelper.LogFailureAudit(string.Format("The username {0} does not exist.", header.Username));
throw new SoapException(string.Empty, SoapException.ClientFaultCode, "Security");
}
else
{
// we found the user, verify the password hash by compare the Salt + PasswordHash
DataRow dataRow = dataSet.Tables[0].Rows[0];
dbPasswordHash = (string)dataRow["PasswordHash"];
string dbPasswordSalt = (string)dataRow["PasswordSalt"];
// create a hash based on the user's salt and the input password
string passwordHash = HashString(dbPasswordSalt + header.Password);
// does the computed hash match the database hash?
if (string.Compare(dbPasswordHash, passwordHash) != 0)
{
EventLogHelper.LogFailureAudit(string.Format("The password for the username {0} was incorrect.", header.Username));
throw new SoapException(string.Empty, SoapException.ClientFaultCode, "Security");
}
}
return dbPasswordHash;
}
4. ×îºó¿Í»§¶ËÔÚµ÷ÓÃÒªÇó±êÍ·µÄ·½·¨Ö®Ç°£¬ÐèÖ±½ÓÔÚ´úÀíÀàÉÏÉèÖñêÍ·.
IssueVision µÄSmartClient¶ËµÄWebServicesLayerÀàÀ´µ÷ÓôËWeb Services
WebServicesLayer.cs³ÌÐòƬ¶ÏÈçÏÂ:
private static IssueVisionServices GetWebServiceReference(string username, string password)
{
IssueVisionServices dataService = new IssueVisionServices();
//
CredentialSoapHeader header = new CredentialSoapHeader();
header.Username = username;
header.Password = password;
dataService.CredentialSoapHeaderValue = header;
//
InitWebServiceProxy(dataService);
return dataService;
}
ͨ¹ýÒÔÉϲ½Öè¾Í¿ÉÒÔÍê³ÉWeb Services×Ô¶¨ÒåÉí·ÝÑéÖ¤ÁË.IssueVisionÖл¹ÓкܶàÏà¹ØµÄ²Ù×÷,ÒòΪÔÚÕâÀïÖ»ÊÇÌÖÂÛÒ»ÏÂSoapHeaderµÄÓ÷¨,¾Í²»ÔÚÁоÙÁË.
±ÉÈ˼ûʶ¾ÍÕâô¶àÁË,»¶Ó´ó¼ÒÌÖÂÛ,Ìá³öеĿ´·¨.
CopyRight © YellowWee 2004. All Right Reserved.

