Snippets

Stefan de Vogelaere SS Auth Microservice - JWT

Created by Stefan de Vogelaere last modified
using System;
using ServiceStack;
using ServiceStack.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var authClient = new JsonServiceClient("http://localhost:63404/");
            var jwtToken = authClient.Send(new Authenticate
            {
                provider = "credentials",
                UserName = "Stefan",
                Password = "p@55word",
                UseTokenCookie = true
            });
            Console.WriteLine(jwtToken.Dump());

            var result = authClient.Send(new Hello() {Name = "from auth service"});

            var jwtToken2 = authClient.GetTokenCookie(); //From ss-tok Cookie
            Console.WriteLine(jwtToken2);

            var tokenResponse = authClient.Send(new ConvertSessionToToken());
            Console.WriteLine(tokenResponse);
        }
    }

    public class Hello : IReturn<HelloResponse>
    {
        public string Name { get; set; }
    }

    public class HelloResponse
    {
        public string Result { get; set; }
    }
}
using Funq;
using ServiceStack;
using ServiceStack.Auth;
using ServiceStack.Caching;
using ServiceStack.Data;
using ServiceStack.OrmLite;
using WebApplication1.ServiceInterface;

namespace WebApplication1
{
    //VS.NET Template Info: https://servicestack.net/vs-templates/EmptyAspNet
    public class AppHost : AppHostBase
    {
        /// <summary>
        ///     Base constructor requires a Name and Assembly where web service implementation is located
        /// </summary>
        public AppHost()
            : base("WebApplication1", typeof(MyServices).Assembly)
        {
        }

        /// <summary>
        ///     Application specific configuration
        ///     This method should initialize any IoC resources utilized by your web service classes.
        /// </summary>
        public override void Configure(Container container)
        {
            //Store UserAuth in SQL Server
            var dbFactory = new OrmLiteConnectionFactory(
                "[redacted]",
                SqlServerDialect.Provider);

            container.Register<IDbConnectionFactory>(dbFactory);
            container.Register<IAuthRepository>(c =>
                new OrmLiteAuthRepository(dbFactory) {UseDistinctRoleTables = true});

            //Create UserAuth RDBMS Tables
            container.Resolve<IAuthRepository>().InitSchema();

            //Also store User Sessions in SQL Server
            container.RegisterAs<OrmLiteCacheClient, ICacheClient>();
            container.Resolve<ICacheClient>().InitSchema();


            var privateKey = RsaUtils.CreatePrivateKeyParams(RsaKeyLengths.Bit2048);
            var publicKey = privateKey.ToPublicRsaParameters();
            var privateKeyXml = privateKey.ToPrivateKeyXml();
            var publicKeyXml = privateKey.ToPublicKeyXml();

             // just for testing, create a privateKeyXml on every instance
            Plugins.Add(new AuthFeature(() => new AuthUserSession(),
                new IAuthProvider[]
                {
                    new JwtAuthProvider
                    {
                        HashAlgorithm = "RS256",
                        PrivateKeyXml = privateKeyXml
                    },
                    new CredentialsAuthProvider() 
                }));


            Plugins.Add(new RegistrationFeature());

            // uncomment to create a first new user

            //var authRepo = GetAuthRepository();
            //authRepo.CreateUserAuth(new UserAuth
            //{
            //    Id = 1,
            //    UserName = "Stefan",
            //    FirstName = "First",
            //    LastName = "Last",
            //    DisplayName = "Display",
            //}, "p@55word");

        }
    }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.