473,327 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

soap authentication and custom method in global.asax

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
1 6287
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: JP | last post by:
I need to be able to trap errors at the application level. I added this code to the Global.asax file. The code I wrote is supposed to get the last error that was generated and write to the event...
4
by: Arjen | last post by:
Hi, I load my website settings inside the global.asax file. For this I have to use the context, to get the roles from the current browsing client. The problem is that there is no context at...
3
by: Joe Reazor | last post by:
I understand how Web.Config inheritance works between a parent application and sub applications under the parent. But what I was wondering was if there was a similar way to do the same thing for...
5
by: bcharles | last post by:
My company has an existing web app. It was written by someone who has left. It generates an error if authentication fails and sends an email to the email address of the person who left. I want it...
15
by: randyr | last post by:
I am developing an asp.net app based on a previous asp application. in the asp applications global.asa file I had several <object id="id" runat="server" scope="scope" class="comclass"> tags for...
3
by: John Holgerson | last post by:
Hi, is it possible to get informations about the exception that occured in that method?? (global.asax) protected void Application_Error(Object sender, System.EventArgs e) { } thanks for...
1
by: | last post by:
Hi all, Is it possible to have custom function/subroutines in global.asax? e.g. DoMyWork(i as integer) etc etc. If yes, how can I access this code in my ..aspx pages? I know I can put this into...
7
by: Jonas | last post by:
Hi! I have an Application_Error method in global.asax that uses Server.Transfer to move execution to a custom error page. This works fine when an exception is thrown in one of the aspx or ascx...
2
by: tshad | last post by:
I am setting up Authentication that I want to put in multiple Web Sites on my server. I found a good article on this and am looking at moving my code from my Global.asax file to an HTTP Module. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.