When I started with ServiceStack I wanted to develop a simple web app with these 3 components:
SelftHost: It is great to just copy the output of the project and run it on Linux with Mono without hassle
Razor: ServiceStack only without any other dependency
Webform authentication: by far most common authentication method for simple web projects (eventually using SSL for security)
ServiceStack wiki is great and I quickly discovered the Razor Rockstars project that brings a lot of answers but it was, at first, not easy to see what I really needed to put together
What we will be building
A simple web app retrieving car license plate numbers. This could be for example all car present in a parking that a registered user can access.
For simplicity of the code we will put user infos and sensor data directly in the code.
We will be doing things in this order:
Generate the basic web service to retrieve the data
Add html views
Add Authentication
Get started : Self-Host
Create a console application and install ServiceStack from NuGet.
Create your service and the DTOs
Then the AppHost and the main
Creating Razor views
Install ServiceStack.Razor from NuGet.
It is not installed by default with other ServiceStack packages.
Create a folder Views in your solution and put inside _Layout.cshtml and cars.cshtml.
Then put the an other pages called Login.cshtml at the root of the project, outside the Views.
The reason for that is that we want to be able to call the login page directly. The cars view will be rendered through the service call.
Select all .cshtml file right click and see the properties then select **Copy if newer so that the files will be copied in the project output during generation.
Input the following contents:
_Layout.cshtml
login.cshtml
We need to input the following config in App.config for Razor views
Now if you start the project and browse to http://localhost:8090/login you should see the login page !
And if you input some login and password the result will be:
This is normal since we did not activate any Auth module yet :)
Adding Auhtentication
Update your AppHost Configure method as follow:
And if you try again to login, this time it is ConfigurationErrorsException which tells you that a repository is needed to authenticate.
Let’s create an InMemory Repo with a user:
And now if you try to login with demo/demo it works !
The login.cshtml webform is posting the user/password to /Auth/Credentials which is registered by ServiceStack
Then the following input declare the continue variable used for the redirection after login.
Still a few issues pending though.
We can access the service /cars directly. We need to avoid that
We want to be able to logout
We want to see the /cars result in our own template
Protect the service access
Just decorate the service with Authenticate attribute and you are done
Logout
Just call /auth/logout. It is as simple as that. A simple link is enough.
See the service result in our template
So far we did not touch the cars.cshtml. Let’s create it:
Notice that we added the logout link.
Final test
And that’s it. We have web form authentication working with a Self-Host ServiceStack instance. We can render the Service response in Razor view with strong typed view model.