Jak zaimplementować uwierzytelnianie WebServiceHost?

Wiem, że uwierzytelnianie w klasie webservicehost nie jest w pełni zgodne ze standardami uwierzytelniania (zwraca 403 zabronione, a nie monituje o kolejny zestaw poświadczeń, gdy użytkownik wprowadzi nieprawidłowe poświadczenia).

Nadal chciałbym zaimplementować to podstawowe uwierzytelnianie (nazwa użytkownika i hasło na początku sesji, niepotrzebne HTTPS - patrz rysunek poniżej), ponieważ odpowiada to moim potrzebom w małym projekcie domowym.

Kod, który mam dla mojej usługi, jest następujący:

Imports System.IO
Imports System.Text
Imports System.ServiceModel
Imports System.ServiceModel.Web
Imports System.ServiceModel.Channels

<ServiceContract()>
Public Class myService
    <OperationContract(), WebGet(UriTemplate:="/xml/{argument1}/{argument2}")>
    Public Function XML(argument1 As String, argument2 As String) As Stream
        requestCounter += 1
        Console.WriteLine("xml data request at " & DateTime.Now.ToString() & ", request count= " & requestCounter)
        Console.WriteLine(WebOperationContext.Current.IncomingRequest.UserAgent.ToString())
        Return _ReturnXML("<xmlresponse><data><argument1>" & argument1 & "</argument1><argument2>" & argument2 & "</argument2></data><server><serverlivesince>" & serverStart.ToString() & "</serverlivesince><pageservetime>" & DateTime.Now.ToString() & "</pageservetime><requestcount>" & requestCounter & "</requestcount></server></xmlresponse>")
        'returns the first two parameters, and the time and date
    End Function

    Private Shared Function _ReturnXML(_result As String) As Stream
        Dim data = Encoding.UTF8.GetBytes(_result)

        WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml; charset=utf-8"
        WebOperationContext.Current.OutgoingResponse.ContentLength = data.Length

        Return New MemoryStream(data)
    End Function
End Class

Następnie mam podobny kod do zwracania kodu HTML, a także akceptowania innych kombinacji parametrów.

W mojej klasie głównej stworzyłem instancję i otworzyłem tę usługę jako:

Dim varWebService = New WebServiceHost(GetType(MyWebService), New Uri("http://0.0.0.0/"))
varWebService.Open()

Czy ktoś może dostarczyć mi kod do implementacji tego prostego uwierzytelniania? Lub wskaż mi dokładny samouczek? Dzięki za pomoc

questionAnswers(1)

yourAnswerToTheQuestion