I am trying to implement basic authentication to ASP.NET Web API project, I have created a class BasicAuthHttpModule implementing IHttpModule successfully, but when I am trying to register the module in web.config file, it is always returning back an error:
Server Error in '/' Application.
Could not load type 'testIhttpModule.Modules.BasicAuthHttpModule' from assembly 'testIhttpModule'
The funny part that when I made the same exact project in C#, it is working like a charm! But the business need to use VB.NET.
Could anybody help me solve the issue? Here is my IHttpModule class definition and the web.config part for registering the module:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net.Http.Headers
Imports System.Security.Principal
Imports System.Text
Imports System.Threading
Imports System.Web
Namespace testIhttpModule.Modules
Public Class BasicAuthHttpModule
Implements IHttpModule
Private Const Realm As String = "My Realm"
Public Sub Init(ByVal context As HttpApplication) Implements IHttpModule.Init
AddHandler context.AuthenticateRequest, AddressOf OnApplicationAuthenticateRequest
AddHandler context.EndRequest, AddressOf OnApplicationEndRequest
End Sub
Private Shared Sub SetPrincipal(ByVal principal As IPrincipal)
Thread.CurrentPrincipal = principal
If HttpContext.Current IsNot Nothing Then
HttpContext.Current.User = principal
End If
End Sub
Private Shared Function CheckPassword(ByVal username As String, ByVal password As String) As Boolean
Return (username = "admin" AndAlso password = "admin")
End Function
Private Shared Sub AuthenticateUser(ByVal credentials As String)
Try
Dim encoding As Encoding = Encoding.GetEncoding("iso-8859-1")
credentials = encoding.GetString(Convert.FromBase64String(credentials))
Dim separator As Integer = credentials.IndexOf(":"c)
Dim name As String = credentials.Substring(0, separator)
Dim password As String = credentials.Substring(separator + 1)
If CheckPassword(name, password) Then
Dim identity = New GenericIdentity(name)
SetPrincipal(New GenericPrincipal(identity, Nothing))
Else
HttpContext.Current.Response.StatusCode = 401
End If
Catch __unusedFormatException1__ As FormatException
HttpContext.Current.Response.StatusCode = 401
End Try
End Sub
Private Shared Sub OnApplicationAuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim request = HttpContext.Current.Request
Dim authHeader = request.Headers("Authorization")
If authHeader IsNot Nothing Then
Dim authHeaderVal = AuthenticationHeaderValue.Parse(authHeader)
If authHeaderVal.Scheme.Equals("basic", StringComparison.OrdinalIgnoreCase) AndAlso authHeaderVal.Parameter IsNot Nothing Then
AuthenticateUser(authHeaderVal.Parameter)
End If
End If
End Sub
Private Shared Sub OnApplicationEndRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim response = HttpContext.Current.Response
If response.StatusCode = 401 Then
response.Headers.Add("WWW-Authenticate", String.Format("Basic realm=""{0}""", Realm))
End If
End Sub
Public Sub Dispose() Implements IHttpModule.Dispose
End Sub
End Class
End Namespace
And this is the web.config register part:
<system.webServer>
<modules>
<add name="BasicAuthHttpModule" type="testIhttpModule.Modules.BasicAuthHttpModule,testIhttpModule" />
</modules>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0"/>
<remove name="OPTIONSVerbHandler"/>
<remove name="TRACEVerbHandler"/>
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers>
</system.webServer>
After googling the issue more than 16 hours, I have ended up with some post advising to create folder App_Code and to move the class into it. I was able to load the module, but another error occurs:
Here is my project hierarchy:
I am trying to register the module in web.config file, I am expecting the project will run without any error, but the result always an error
Server Error in '/' Application.
Could not load type 'testIhttpModule.Modules.BasicAuthHttpModule' from assembly 'testIhttpModule'.