在.NET中建立Web service的安全措施,第三部分

ZDNet软件频道 时间:2002-11-28 作者:Techmail 代码页 |  我要评论()
本文关键词:
<WebMethod()> Public Function HelloWorld() As String
Try
Dim requestCtx As SoapContext = HttpSoapContext.RequestContext

If requestCtx Is Nothing Then
Throw New ApplicationException("Only SOAP requests are
allowed")
End If

If Validate(requestCtx) = False Then
Throw New SoapException("Security token specified was not
valid", New System.Xml.XmlQualifiedName("Bad.Tokens",
http://localhost/securitytester/"))
End If

Dim responseCtx As SoapContext = HttpSoapContext.ResponseContext
responseCtx.Timestamp.Ttl = 60000

Return "HelloWorld"

Catch ex As Exception
Throw ex
End Try
End Function

Private Function Validate(ByVal Ctx As SoapContext) As Boolean
' If no security tokens are present, return false immediately
If Ctx.Security.Tokens.Count = 0 Then Return False

Dim valid As Boolean
Dim Count As Int16 = 0

For Count = 0 To Ctx.Security.Elements.Count - 1
Dim signature As Signature = Ctx.Security.Elements(Count)

If Not signature Is Nothing And signature.IncludesSoapBody Then
Dim UserToken As UsernameToken = signature.SecurityToken
If Not UserToken Is Nothing Then
Dim UserName As String = UserToken.Username
' Empty code block, this is where you would validate the
UserName value itself against a datastore
valid = True
End If
End If
Next

Return valid

End Function 

百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134