




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
在调用Web Serivices时,往往需要身份验证,使得通过验证的用户才能调用你Web Serivices中的方法.当然你可以通过将参数添加到每个需要自定义身份验证方案的Web services方法中去,这需要花费很大的精力.IssueVision 中使用了非常常用而且有效便捷的方法-使用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.csusing 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 都可以使用.(默认情况下,标头是必需的,但也可以定义可选标头) 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 users 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.Tables0.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.Tables0.Rows0; dbPasswordHash = (string)dataRowPasswordHash; string dbPasswordSalt = (string)dataRowPasswordSalt; / create a hash based on the users 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(); / CredentialSoapHeaderheader = new CredentialSoapHeader(); header.Username = username; header.Password = password; dataService.CredentialSoapHeaderValue = header; / InitWebServiceProxy(dataService); return dataService; 通过以上步骤就可以完成Web Services自定义身份验证了.IssueVision中还有很多相关的操作,因为在这里只是讨论一下SoapHeader的用法,就不在列举了. WebService中如何使用soapheader1. 首先需要在服务中定义一个从 SOAPHeader 派生的类,表示传入 SOAP 标头的数据.public class SecurityHeader: System.Web.Services.Protocols.SoapHeader public string userName; public string passWord; public string key; public SecurityHeader() 2. 将SecurityHeader类作为webservice类的一个属性公开,并在WebMethod里使用SecurityHeader 这样就允许客户使用这个SoapHeader了。public class Map : System.Web.Services.WebService public SecurityHeader keyHeader; public Map() WebMethod,SoapHeader(keyHeader) public string HelloWorld() /这里我们可以获得客户传来的header值按需要做验证等处理 string keyValue = keyHeader.key ; return keyValue; 客户端使用SoapHeader的方法如下(调用之前先声明webreference MapClient): MapService.MapClient.SecurityHeader header = new MapService.MapClient.SecurityHeader(); header.userName = userName; h
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 内蒙古自治区丰镇市第一中学2024-2025学年高二下学期期中考试政治试题B卷(原卷版+解析版)
- 国际货品进出口贸易合同
- 智慧交通运输管理平台开发与服务协议
- IT技术支持与服务提供合同细节规定事项清单
- 物业内勤的工作总结(14篇)
- 音内容制作及版权转让协议
- 2025福建南安市首创水务有限责任公司招聘6人笔试参考题库附带答案详解
- 2025福建武夷碳产业投资有限公司招聘2人笔试参考题库附带答案详解
- 2025浙江省安全生产科学研究有限公司招聘15人笔试参考题库附带答案详解
- 2025年甘肃省庆阳市新庄煤矿面向社会招聘生产性灵活用工206人笔试参考题库附带答案详解
- 品质主管面试题及答案
- 中国精神课件
- 2025年福建福州市电子信息集团有限公司招聘笔试参考题库附带答案详解
- 天津市和平区二十中学2025届学业水平考试化学试题模拟卷(九)含解析
- 篮球智慧树知到期末考试答案章节答案2024年浙江大学
- GB/T 17937-2024电工用铝包钢线
- 多图中华民族共同体概论课件第十一讲 中华一家与中华民族格局底定(清前中期)根据高等教育出版社教材制作
- 2013年高考安徽理科数学试题及答案(word解析版)
- 跨文化交际案例分析【精选文档】
- 国际空运知识
- GreedyRabbit贪吃的兔子PPT课件
评论
0/150
提交评论