So I have been using Nancy and my IIS is configured to use windows authentication.

Now how do I get the current authenticated user onto my webpage.

But first of all how do I get the name of the current authenticated user.

Well that was the simple part.

vb.net
1
HttpContext.Current.User.Identity.Name
HttpContext.Current.User.Identity.Name

And there you have it.

Now we all know that we can do this in Nancy with Razor to get the current logged in user when using formsauthentication or basic authentication.

vb.net
1
Url.RenderContext.Context.CurrentUser
Url.RenderContext.Context.CurrentUser

Of course we could do this in our Module.

vb.net
1
Context.CurrentUser = New User With {.UserName = HttpContext.Current.User.Identity.Name}
Context.CurrentUser = New User With {.UserName = HttpContext.Current.User.Identity.Name}

And that would work, but it would become very tedious to repeat this for every module we have. So we have to look for a better way of doing this. And of course there is.

We can make our own bootstrapper and change it in the nancy pipleine.

vb.net
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Imports Nancy
Imports Nancy.Security
 
Public Class BootStrapper
    Inherits DefaultNancyBootstrapper
 
    Protected Overrides Sub ApplicationStartup(container As TinyIoc.TinyIoCContainer, pipelines As Nancy.Bootstrapper.IPipelines)
        MyBase.ApplicationStartup(container, pipelines)
        pipelines.AfterRequest.AddItemToEndOfPipeline(Sub(ctx)
                                                          ctx.CurrentUser = New User With {.UserName = HttpContext.Current.User.Identity.Name}
                                                      End Sub)
    End Sub
 
    Private Class User
        Implements IUserIdentity
 
        Public Property Claims As IEnumerable(Of String) Implements IUserIdentity.Claims
 
        Public Property UserName As String Implements IUserIdentity.UserName
    End Class
 
End Class
Imports Nancy
Imports Nancy.Security

Public Class BootStrapper
    Inherits DefaultNancyBootstrapper

    Protected Overrides Sub ApplicationStartup(container As TinyIoc.TinyIoCContainer, pipelines As Nancy.Bootstrapper.IPipelines)
        MyBase.ApplicationStartup(container, pipelines)
        pipelines.AfterRequest.AddItemToEndOfPipeline(Sub(ctx)
                                                          ctx.CurrentUser = New User With {.UserName = HttpContext.Current.User.Identity.Name}
                                                      End Sub)
    End Sub

    Private Class User
        Implements IUserIdentity

        Public Property Claims As IEnumerable(Of String) Implements IUserIdentity.Claims

        Public Property UserName As String Implements IUserIdentity.UserName
    End Class

End Class

So I added this to the AfterRequest hook And that changes the currentuser on every request for me.

I also created a class User because CurrentUser needs a class that implements IUserIdentity.

Cool huh.