I've created a WCF Service in an existing ASP.NET Website and implemented a custom UserNamePasswordValidator to check the client's credentials against our database.
My problem occurs when I try to set the HTTPContext's User to a custom principle. It works all of the time, except the first call after recompiling the website. In that situation, HTTPContext.Current is null, throwing an exception. All subsequent calls work properly.
The context exists in the AuthenticateRequest event handler and in the service's method for all requests and ASP.NET Compatibility is enabled for the website and required for the service.
Here is the code for your reference:
Try
If HttpContext.Current Is Nothing OrElse Not HttpContext.Current.Request.IsAuthenticated Then
Dim sec As New Security.Login
Dim userInfo As Security.Login.LoginInfo = sec.AuthenticateUser(userName, password)
HttpContext.Current.User = CustomPrinciple(userInfo)
End If
Catch ex As Exception
Throw New System.ServiceModel.FaultException("Unknown Username or Incorrect Password")
End Try
Thanks.