Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Web Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Nancy and custom error pages.Angularjs and ng-grid »
    comments

    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.

    1. 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.

    1. Url.RenderContext.Context.CurrentUser

    Of course we could do this in our Module.

    1. 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.

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

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    InstapaperVote on HN

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)