Introduction In the first part I showed you how to configure ServiceStack and in the second part I showed you how to read from the ServiceStack service with Easyhttp. And now it is time to give Santa the opportunity to add some children. ServiceStack I want to use the same request for my Post as I did for my Get. This means I will need to add LastName to the request. Imports ServiceStack.ServiceInterface Namespace Request <Authenticate> Public Class GoodOrBadRequest Public Property Good As Boolean? Public Property LastName As String End Class End Namespace``` And then we need to add the Post implementation to our service. ```vbnet Imports SantaGoodAndBad.Response Imports SantaGoodAndBad.Request Imports ServiceStack.ServiceInterface Namespace DefaultNamespace.Service Public Class GoodOrBadService Inherits RestServiceBase(Of GoodOrBadRequest) Dim children As List(Of Child) Public Sub New() MakeChildren() End Sub Public Overrides Function OnPost(request As GoodOrBadRequest) As Object children.Add(New Child With {.LastName = request.LastName, .Good = request.Good}) Return children End Function Public Overrides Function OnGet(request As GoodOrBadRequest) As Object If request.Good.HasValue Then Return children.Where(Function(child) child.Good.Value = request.Good.Value).ToList Else Return children End If End Function Private Function MakeChildren() As IList(Of Child) children = New List(Of Child) children.Add(New Child With {.LastName = "Baes", .Good = True}) children.Add(New Child With {.LastName = "Hariri", .Good = False}) children.Add(New Child With {.LastName = "Hanselman", .Good = True}) children.Add(New Child With {.LastName = "Krueger", .Good = False}) Return children End Function End Class End Namespace``` The above works just fine, trust me. However, the client changed their minds, they only want Santa to be able to read and add children to the list and the elfs can read the list but not add children to the list. For this to work I have to create an elf account with a role of elfs and a santa with roles of santa and elfs. We do this in the apphost for now. ```vbnet Imports ServiceStack.CacheAccess.Providers Imports ServiceStack.CacheAccess Imports ServiceStack.ServiceInterface.Auth Imports SantaGoodAndBad.Request Imports SantaGoodAndBad.DefaultNamespace.Service Imports ServiceStack.ServiceInterface Imports ServiceStack.WebHost.Endpoints Public Class AppHost Inherits AppHostHttpListenerBase Public Sub New() MyBase.New("Good or bad", GetType(GoodOrBadService).Assembly) End Sub Public Overrides Sub Configure(container As Funq.Container) Routes.Add(Of GoodOrBadRequest)("/GoodOrBad") _ .Add(Of GoodOrBadRequest)("/GoodOrBad/{Good}") Plugins.Add(New AuthFeature(Function() New AuthUserSession(), New AuthProvider() {New CredentialsAuthProvider()})) container.Register(Of ICacheClient)(New MemoryCacheClient()) Dim userRep = New InMemoryAuthRepository() container.Register(Of IUserAuthRepository)(userRep) Dim hash As String Dim salt As String Dim s = New SaltedHash() s.GetHashAndSaltString("test", hash, salt) userRep.CreateUserAuth(New UserAuth With {.Id = 1, .DisplayName = "Santa Claus", .Email = "Santa@Claus.com", .UserName = "santa", .FirstName = "Kris", .LastName = "Kringle", .PasswordHash = hash, .Salt = salt, .Roles = New List(Of String) From {"Santa", "Elfs"}}, "jingle") userRep.CreateUserAuth(New UserAuth With {.Id = 2, .DisplayName = "Elf 1", .Email = "Elf1@Claus.com", .UserName = "elf1", .FirstName = "Elf", .LastName = "One", .PasswordHash = hash, .Salt = salt, .Roles = New List(Of String) From {"Elfs"}}, "bells") End Sub End Class``` And we need to tell our request which roles are allowed to do what. ```vbnet Imports ServiceStack.ServiceInterface Namespace Request <Authenticate> <RequiredRole(ApplyTo.Get, "Elfs")> <RequiredRole(ApplyTo.Post, "Santa")> Public Class GoodOrBadRequest Public Property Good As Boolean? Public Property LastName As String End Class End Namespace``` Yep that was pretty simple. Now to test this. ## Easyhttp I added a method to do the post and then read the results and I added another Authorize for easy testing. Here is the complete copy pasteable code ```vbnet Option Strict Off Imports EasyHttp.Http Module Module1 Sub Main() Dim response As HttpResponse InitializeClient() PrintChild(DoGet("http://localhost:58241/GoodOrBad", Nothing)) Console.WriteLine("") AuthorizeSanta() PrintChild(DoGet("http://localhost:58241/GoodOrBad", Nothing)) Console.WriteLine("") PrintChild(DoPost("http://localhost:58241/GoodOrBad", New With {.LastName = "Stack", .Good = False})) Console.WriteLine("") LogOut() AuthorizeElf1() PrintChild(DoGet("http://localhost:58241/GoodOrBad", Nothing)) Console.WriteLine("") PrintChild(DoPost("http://localhost:58241/GoodOrBad", New With {.LastName = "Hamilton", .Good = False})) Console.WriteLine("") LogOut() PrintChild(DoGet("http://localhost:58241/GoodOrBad", Nothing)) Console.WriteLine("") Console.ReadLine() End Sub Private Sub PrintChild(ByVal children As Object) If children IsNot Nothing Then For Each child In children If child.Good = False Then Console.ForegroundColor = ConsoleColor.Red Else Console.ForegroundColor = ConsoleColor.Green End If Console.WriteLine(child.LastName) Next End If Console.ForegroundColor = ConsoleColor.White End Sub Private Function DoGet(ByVal url As String, ByVal parameters As Object) As Object Dim response As HttpResponse = Nothing If parameters IsNot Nothing Then response = http.Get(url, parameters) Else response = http.Get(url) End If If response.StatusCode = Net.HttpStatusCode.Unauthorized Then Console.WriteLine("Unauthorized") response = Nothing End If If response IsNot Nothing Then Return response.DynamicBody Else Return Nothing End If End Function Private Function DoPost(ByVal url As String, ByVal parameters As Object) As Object Dim response As HttpResponse = Nothing If parameters IsNot Nothing Then response = http.Post(url, parameters, HttpContentTypes.ApplicationJson) Else response = Nothing End If If response.StatusCode = Net.HttpStatusCode.Unauthorized Then Console.WriteLine("Unauthorized") response = Nothing End If If response IsNot Nothing Then Return response.DynamicBody Else Return Nothing End If End Function Private Sub AuthorizeSanta() Dim response = http.Post("http://localhost:58241/auth", New With {.UserName = "santa", .Password = "jingle"}, HttpContentTypes.ApplicationJson) http.Request.Cookies = response.Cookie Console.WriteLine(response.DynamicBody.SessionId) Console.WriteLine(response.DynamicBody.UserName) Console.WriteLine("") End Sub Private Sub AuthorizeElf1() Dim response = http.Post("http://localhost:58241/auth", New With {.UserName = "elf1", .Password = "bells"}, HttpContentTypes.ApplicationJson) http.Request.Cookies = response.Cookie Console.WriteLine(response.DynamicBody.SessionId) Console.WriteLine(response.DynamicBody.UserName) Console.WriteLine("") End Sub Private Sub LogOut() Dim response = http.Get("http://localhost:58241/auth/logout") http.Request.Cookies = response.Cookie End Sub Private http As HttpClient Private Sub InitializeClient() If http Is Nothing Then http = New HttpClient() http.Request.Accept = HttpContentTypes.ApplicationJson End If End Sub End Module And here is the result.
This is an archive of the posts published to LessThanDot from 2008 to 2018, over a decade of useful content. While we're no longer adding new content, we still receive a lot of visitors and wanted to make sure the content didn't disappear forever.
A couple of weeks ago someone asked me why they should upgrade to MS SQL Server 2012. I named a bunch of reasons and only remembered afterwards that the possibility to use Windows Server Core could also be a surplus. Just 2 days later this Case for Core post from Jeremiah Peschka (blog | twitter) made me realize there was a lot of work for me before I could say that running MS SQL Server 2012 on Windows Server Core. I already read the book Microsoft SQL Server 2008 Administration with Windows PowerShell and reviewed it here.So I decided to try to avoid the usage of Remote Desktop as much as possible and finally start to use PowerShell on a daily basis. One of the first things I needed to do was the restore of a database on another server. So before I could start the restore I needed to check the servers’ disk configuration and available space. So let’s see how to do this with PowerShell.
Introduction So this week I wanted to setup a webservice that also included authentication. As one usually does this time of the year, Santa needs his data to be protected so that the children can’t see which gifts they will receive. I decided to help Santa by using ServiceStack and Easyhttp. And I described the servicestack in [the first part of this post][1]. Easyhttp First of all we need an httpclient.
Introduction So this week I wanted to setup a webservice that also included authentication. As one usually does this time of the year, Santa needs his data to be protected so that the children can’t see which gifts they will receive. I decided to help Santa by using [ServiceStack][1] and [Easyhttp][2]. ServiceStack Lets start by creating our server first. For this I make a new Empty ASP.Net project. And I change the web.config to this.
It's over. I still can't believe it. Something that I've worked on so hard and cared for 6 months is over. So many great things happened during the day, and so many great people were involved. I'm going to use this space to try to thank everyone, and give a respectful remembrance to the day of my life: Sept 22, 2012 – SQL Saturday #160 day. So many people to thank…. Before I begin, I know I'm going to mess up and forget someone, please believe me when I say that it's not on purpose. The following Thank YOUs are not in any specific order.
There are many great things to say about this past weekend’s SQL Saturday in Minnesota. First, the organizers and volunteers put together a perfectly executed event. Being an organizer of many SQL Saturday events, I know the pain and stress that goes into the actual day of the event due to things simply not going right. Well, if there was something that didn’t go right, stress compounded or any pain inflicted, I can tell the organizers, from my side of things, the event was put together and executed perfectly. A special thanks to them!
Sometime in the past I made a blogpost on [how I made a resharper plugin][1]. First warning. Running your tests will be slow at best. This is because the sdk creates the whole visual studio solution in memory when you run your tests. You can find [all the code on github][2]. The resharper SDK makes it “easy” for you to test. They have automated a lot and you just have to do some simple things and all the magic will be done for you.
In the final installment of my series on SQL window functions, we will explore using analytic functions. Analytic functions were introduced in SQL Server 2012 with the expansion of the OVER clause capabilities. Analytic functions fall in to two primary categories: values at a position and percentiles. Four of the functions, LAG, LEAD, FIRST_VALUE and LAST_VALUE find a row in the partition and returns the desired value from that row. CUME_DIST and PERCENT_RANK break the partition into percentiles and return a rank value for each row. PERCENTILE_CONT and PERCENTILE_DISC a value at the requested percentile in the function for each row. All of the functions and examples in this blog will only work with SQL Server 2012.
Dear people on the VS team, So in VS2012 they added the preview tab. So when you singleclick a file in the solution explorer will open the file in preview mode. There is only one of these tabs so when you single click another file it will just “close” the previous one and show the new one. This way you don’t have have hundreds of files open that you never wanted open in the first place.
Perl::Critic is great. If you haven’t tried it, you should. It can help you improve the quality of your code no end. I wrote this script to make it easier to view the feedback in the context of the code itself. When run in a directory containing Perl code it’ll create a critic_html directory containing index.html summarizing the results: Clicking through to any of the files will give you all the violations found by Perl::Critic, inline with the code: