472,785 Members | 1,235 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,785 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 6217
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?

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.