By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

soap authentication and custom method in global.asax

P: n/a
Stu
Hi,

Im using vis studio 2003 and I think wse is out of the question as clients
could be using java which doesnt support it. So I managed to find some code
which allows you to develop a custom soap header called by using a http
module. The problem Im having is I cannot seem to get the event to raise to
fire off my authenticate method in the global.asax. The module is plumbed in
to my web.config file
Code Below:-
TIA

Stu
Public Class Global
Inherits System.Web.HttpApplication

#Region " Component Designer Generated Code "

Public Sub New()
MyBase.New()

'This call is required by the Component Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container()
End Sub

#End Region

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session is started
End Sub

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
End Sub

Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As
EventArgs)
' Fires upon attempting to authenticate the use
End Sub

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an error occurs
End Sub

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session ends
End Sub

Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application ends
End Sub

Sub Authenticate(ByVal context As HttpContext, ByVal user As String,
ByVal password As String)
' Web Service authenticate
Dim x As Int16
x = 1
End Sub
End Class

Imports System
Imports System.IO
Imports System.Xml
Imports System.web.Services.Protocols

Public NotInheritable Class WebServiceAuthentication
Implements IHttpModule

Public Delegate Sub WebServiceAuthenticationEventHandler(ByVal sender As
[Object], ByVal e As WebServiceAuthenticationEvent)

Private _eventHandler As WebServiceAuthenticationEventHandler = Nothing

Public Event Authenticate As WebServiceAuthenticationEventHandler
' AddHandler(ByVal value As WebServiceAuthenticationEventHandler)

' RemoveHandler(ByVal value As WebServiceAuthenticationEventHandler)
' _eventHandler = value
' End RemoveHandler
' End RaiseEvent
Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
RemoveHandler Authenticate, _eventHandler

End Sub 'Dispose
Public Sub Init(ByVal app As HttpApplication) Implements
System.Web.IHttpModule.Init
AddHandler app.AuthenticateRequest, AddressOf Me.OnEnter

End Sub 'Init
Private Sub OnAuthenticate(ByVal e As WebServiceAuthenticationEvent)
If _eventHandler Is Nothing Then
Return
End If
_eventHandler(Me, e)
If Not (e.User Is Nothing) Then
e.Context.User = e.Principal
End If

AddHandler Authenticate, _eventHandler

RaiseEvent Authenticate(Me, e)

End Sub 'OnAuthenticate

Public ReadOnly Property ModuleName() As String
Get
Return "WebServiceAuthentication"
End Get
End Property

Sub OnEnter(ByVal [source] As [Object], ByVal eventArgs As EventArgs)
Dim app As HttpApplication = CType([source], HttpApplication)
Dim context As HttpContext = app.Context
Dim HttpStream As Stream = context.Request.InputStream

' Save the current position of stream.
Dim posStream As Long = HttpStream.Position

' If the request contains an HTTP_SOAPACTION
' header, look at this message.
If context.Request.ServerVariables("HTTP_SOAPACTION") Is Nothing Then
Return
End If
' Load the body of the HTTP message
' into an XML document.
Dim dom As New XmlDocument
Dim soapUser As String
Dim soapPassword As String

Try
dom.Load(HttpStream)

' Reset the stream position.
HttpStream.Position = posStream

' Bind to the Authentication header.
soapUser = dom.GetElementsByTagName("User").Item(0).InnerText
soapPassword =
dom.GetElementsByTagName("Password").Item(0).Inner Text
Catch e As Exception
' Reset the position of stream.
HttpStream.Position = posStream

' Throw a SOAP exception.
Dim name As New XmlQualifiedName("Load")
Dim soapException As New SoapException("Unable to read SOAP
request", name, e)
Throw soapException
End Try

' Raise the custom global.asax event.
OnAuthenticate(New WebServiceAuthenticationEvent(context, soapUser,
soapPassword))
Return

End Sub 'OnEnter
End Class 'WebServiceAuthenticationModule

Imports System
Imports System.Web
Imports System.Security.Principal

Public Class WebServiceAuthenticationEvent
Inherits EventArgs
Private _IPrincipalUser As Iprincipal
Private _Context As HttpContext
Private _User As String
Private _Password As String
Public Sub New(ByVal context As HttpContext)
_Context = context

End Sub 'New
Public Sub New(ByVal context As HttpContext, ByVal user As String, ByVal
password As String)
_Context = context
_User = user
_Password = password

End Sub 'New

Public ReadOnly Property Context() As HttpContext
Get
Return _Context
End Get
End Property

Public Property Principal() As IPrincipal
Get
Return _IPrincipalUser
End Get
Set(ByVal Value As IPrincipal)
_IPrincipalUser = value
End Set
End Property

Public Overloads Sub Authenticate()
Dim i As New GenericIdentity(User)
Me.Principal = New GenericPrincipal(i, New String(-1) {})

End Sub 'Authenticate

Public Overloads Sub Authenticate(ByVal roles() As String)
Dim i As New GenericIdentity(User)
Me.Principal = New GenericPrincipal(i, roles)

End Sub 'Authenticate

Public Property User() As String
Get
Return _User
End Get
Set(ByVal Value As String)
_User = value
End Set
End Property

Public Property Password() As String
Get
Return _Password
End Get
Set(ByVal Value As String)
_Password = value
End Set
End Property

Public ReadOnly Property HasCredentials() As Boolean
Get
If _User Is Nothing OrElse _Password Is Nothing Then
Return False
End If
Return True
End Get
End Property
End Class 'WebServiceAuthenticationEvent
Jan 12 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
WSE should not be out of the question. There are many tools for languages
from java to cobol to consume basic web services. All you client should need
to do is add custom headers to the soap envelope that are based on the spec
of the version of wes that you are using. We are developing web services
that are using a username token over ssl. the clients app will have to
generate the raw headers to add to the soap envelope before sending teh
request

"Stu" wrote:
Hi,

Im using vis studio 2003 and I think wse is out of the question as clients
could be using java which doesnt support it. So I managed to find some code
which allows you to develop a custom soap header called by using a http
module. The problem Im having is I cannot seem to get the event to raise to
fire off my authenticate method in the global.asax. The module is plumbed in
to my web.config file
Code Below:-
TIA

Stu
Public Class Global
Inherits System.Web.HttpApplication

#Region " Component Designer Generated Code "

Public Sub New()
MyBase.New()

'This call is required by the Component Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container()
End Sub

#End Region

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session is started
End Sub

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
End Sub

Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As
EventArgs)
' Fires upon attempting to authenticate the use
End Sub

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an error occurs
End Sub

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session ends
End Sub

Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application ends
End Sub

Sub Authenticate(ByVal context As HttpContext, ByVal user As String,
ByVal password As String)
' Web Service authenticate
Dim x As Int16
x = 1
End Sub
End Class

Imports System
Imports System.IO
Imports System.Xml
Imports System.web.Services.Protocols

Public NotInheritable Class WebServiceAuthentication
Implements IHttpModule

Public Delegate Sub WebServiceAuthenticationEventHandler(ByVal sender As
[Object], ByVal e As WebServiceAuthenticationEvent)

Private _eventHandler As WebServiceAuthenticationEventHandler = Nothing

Public Event Authenticate As WebServiceAuthenticationEventHandler
' AddHandler(ByVal value As WebServiceAuthenticationEventHandler)

' RemoveHandler(ByVal value As WebServiceAuthenticationEventHandler)
' _eventHandler = value
' End RemoveHandler
' End RaiseEvent
Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
RemoveHandler Authenticate, _eventHandler

End Sub 'Dispose
Public Sub Init(ByVal app As HttpApplication) Implements
System.Web.IHttpModule.Init
AddHandler app.AuthenticateRequest, AddressOf Me.OnEnter

End Sub 'Init
Private Sub OnAuthenticate(ByVal e As WebServiceAuthenticationEvent)
If _eventHandler Is Nothing Then
Return
End If
_eventHandler(Me, e)
If Not (e.User Is Nothing) Then
e.Context.User = e.Principal
End If

AddHandler Authenticate, _eventHandler

RaiseEvent Authenticate(Me, e)

End Sub 'OnAuthenticate

Public ReadOnly Property ModuleName() As String
Get
Return "WebServiceAuthentication"
End Get
End Property

Sub OnEnter(ByVal [source] As [Object], ByVal eventArgs As EventArgs)
Dim app As HttpApplication = CType([source], HttpApplication)
Dim context As HttpContext = app.Context
Dim HttpStream As Stream = context.Request.InputStream

' Save the current position of stream.
Dim posStream As Long = HttpStream.Position

' If the request contains an HTTP_SOAPACTION
' header, look at this message.
If context.Request.ServerVariables("HTTP_SOAPACTION") Is Nothing Then
Return
End If
' Load the body of the HTTP message
' into an XML document.
Dim dom As New XmlDocument
Dim soapUser As String
Dim soapPassword As String

Try
dom.Load(HttpStream)

' Reset the stream position.
HttpStream.Position = posStream

' Bind to the Authentication header.
soapUser = dom.GetElementsByTagName("User").Item(0).InnerText
soapPassword =
dom.GetElementsByTagName("Password").Item(0).Inner Text
Catch e As Exception
' Reset the position of stream.
HttpStream.Position = posStream

' Throw a SOAP exception.
Dim name As New XmlQualifiedName("Load")
Dim soapException As New SoapException("Unable to read SOAP
request", name, e)
Throw soapException
End Try

' Raise the custom global.asax event.
OnAuthenticate(New WebServiceAuthenticationEvent(context, soapUser,
soapPassword))
Return

End Sub 'OnEnter
End Class 'WebServiceAuthenticationModule

Imports System
Imports System.Web
Imports System.Security.Principal

Public Class WebServiceAuthenticationEvent
Inherits EventArgs
Private _IPrincipalUser As Iprincipal
Private _Context As HttpContext
Private _User As String
Private _Password As String
Public Sub New(ByVal context As HttpContext)
_Context = context

End Sub 'New
Public Sub New(ByVal context As HttpContext, ByVal user As String, ByVal
password As String)
_Context = context
_User = user
_Password = password

End Sub 'New

Public ReadOnly Property Context() As HttpContext
Get
Return _Context
End Get
End Property

Public Property Principal() As IPrincipal
Get
Return _IPrincipalUser
End Get
Set(ByVal Value As IPrincipal)
_IPrincipalUser = value
End Set
End Property

Public Overloads Sub Authenticate()
Dim i As New GenericIdentity(User)
Me.Principal = New GenericPrincipal(i, New String(-1) {})

End Sub 'Authenticate

Public Overloads Sub Authenticate(ByVal roles() As String)
Dim i As New GenericIdentity(User)
Me.Principal = New GenericPrincipal(i, roles)

End Sub 'Authenticate

Public Property User() As String
Get
Return _User
End Get
Set(ByVal Value As String)
_User = value
End Set
End Property

Public Property Password() As String
Get
Return _Password
End Get
Set(ByVal Value As String)
_Password = value
End Set
End Property

Public ReadOnly Property HasCredentials() As Boolean
Get
If _User Is Nothing OrElse _Password Is Nothing Then
Return False
End If
Return True
End Get
End Property
End Class 'WebServiceAuthenticationEvent

Feb 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.