Базовая аутентификация OpenRasta в VB.Net - PullRequest
0 голосов
/ 15 февраля 2012

Задать вопрос - значит ответить на него.

Я искал примеры кода OpenRasta в VB.Net и не смог найти то, что искал. На основе кода C # и превосходного образца базовой аутентификации Скотта Литтлвуда https://github.com/scottlittlewood/OpenRastaAuthSample, я создал образец приложения VB.Net, которое запускает OpenRasta непосредственно на httplistener и реализует базовую аутентификацию.

Чтобы реализовать базовую аутентификацию, наследуйте от IBasicAuthenticator и внедрите Authenticate и Realm:

Public Class YourBasicAuthenticator : Implements IBasicAuthenticator

    Public Function Authenticate(header As OpenRasta.Authentication.Basic.BasicAuthRequestHeader) As OpenRasta.Authentication.AuthenticationResult Implements OpenRasta.Authentication.Basic.IBasicAuthenticator.Authenticate

        If header.Username = "usernamehere" AndAlso _
            header.Password = "passwordhere" Then

            Return New AuthenticationResult.Success("usernamehere")

        Else

            Return New AuthenticationResult.Failed

        End If
    End Function

    Public ReadOnly Property Realm As String Implements OpenRasta.Authentication.Basic.IBasicAuthenticator.Realm
        Get

            Return "Realm"

        End Get
    End Property

End Class

Добавить пользовательские зависимости в пространство ресурсов:

ResourceSpace.Uses.CustomDependency(Of OpenRasta.Authentication.IAuthenticationScheme, OpenRasta.Authentication.Basic.BasicAuthenticationScheme)(OpenRasta.DI.DependencyLifetime.Singleton)

ResourceSpace.Uses.CustomDependency(Of OpenRasta.Authentication.Basic.IBasicAuthenticator, YourBasicAuthenticator)(OpenRasta.DI.DependencyLifetime.Transient)

И украсьте ваши обработчики:

<RequiresAuthentication()> _

Пример приложения VS2010 с помощью bitbucket: https://bitbucket.org/starlogicsh/openrasta-vbsample/wiki/Home

...