OpenRasta working example - Scott Littlewoods Basic Authentication

I am testing the possibility of using OpenRasta as a viable alternative to ASP.NET MVC. However, I ran into a stumbling block regarding authentication.

Let me be clear, "Open Digest Authentication" is currently not an option .

I read that Scott Littlewood created the basic authentication fork for OpenRasta, and I downloaded the source from git and successfully built it.

Now I'm trying to make authentication work, so if anyone has a real working model, I would be very grateful. Here is what I have done so far:

//Authentication.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OpenRasta;
using OpenRasta.Configuration;
using OpenRasta.Authentication;
using OpenRasta.Authentication.Basic;
using OpenRasta.Configuration.Fluent;
using OpenRasta.DI;

namespace myOpenRastaTest.Extensions
{
    public static class ExtensionsToIUses
    {
        public static void BasicAuthentication<TBasicAuthenticator>(this IUses uses) where TBasicAuthenticator : class, IBasicAuthenticator
        {
            uses.CustomDependency<IAuthenticationScheme, BasicAuthenticationScheme>(DependencyLifetime.Transient);

            uses.CustomDependency<IBasicAuthenticator, TBasicAuthenticator>(DependencyLifetime.Transient);
        }
    }

    public class CustomBasicAuthenticator : IBasicAuthenticator
    {
        public string Realm { get { return "stackoverflow-realm"; } }

        public CustomBasicAuthenticator()
        {            
        }

        public AuthenticationResult Authenticate(BasicAuthRequestHeader header)
        {
            /* use the information in the header to check credentials against your service/db */
            if (true)
            {
                return new AuthenticationResult.Success(header.Username);
            }

            return new AuthenticationResult.Failed();
        }
    }
}

Now, to test this, I simply created an instance of CustomBasicAuthenticator in my HomeHandler.cs:

//HomeHandler.cs
using System;
using myOpenRastaTest.Resources;

namespace myOpenRastaTest.Handlers
{
    public class HomeHandler
    {
        public object Get()
        {
            var custAuth = new myOpenRastaTest.Extensions.CustomBasicAuthenticator();

            return new HomeResource();
        }
    }
}

, , , , , , 2 , , OpenRasta, RESTful lingo, :)

, , , asp.net OpenRasta .

...

+3

Source: https://habr.com/ru/post/1773698/


All Articles