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

    « Support for segments in easyhttpNancy and VB.Net: Using easyhttp as our client »
    comments

    Introduction

    Having set up forms authentication for my little project (which is still on github) I found the need to put a login/logout link/thing on my viewpages.
    Since I could not immediately find how to this I asked on the > e and got a swift answer. That answer helped me along, but we still hit a little bump in the road. Here is my journey.

    The view

    In essence it is very simple to make the login/logout link since you are able to use the Nancy context in your view.

    All you need is this.

    1. @If Url.RenderContext.Context.CurrentUser Is Nothing Then
    2.         @<a href="/login">Login</a>
    3.     Else
    4.         @Url.RenderContext.Context.CurrentUser.UserName  
    5.         @<a href="/logout">Logout</a>
    6.     End If

    When a user is not logged in (sometimes also known as logged out) the login link will appear which will guide you to the loginpage. Once the user is logged in we will show his username and the logout link so he can press that and be amazed.

    The VB razor syntax was kind of hard to figure out but nothing a little trail and error won't fix.

    And then you go and try this and notice the nice errormessage you will get.

    With this in the details view.

    Error Details

    Error compiling template: Views/Master.vbhtml

    Errors:
    [BC30652] Line: 8 Column: 0 - Reference required to assembly 'Nancy, Version=0.14.1.0, Culture=neutral, PublicKeyToken=null' containing the type 'Nancy.ViewEngines.IRenderContext'. Add one to your project. (show)
    [BC30652] Line: 11 Column: 0 - Reference required to assembly 'Nancy, Version=0.14.1.0, Culture=neutral, PublicKeyToken=null' containing the type 'Nancy.ViewEngines.IRenderContext'. Add one to your project. (show)

    Details:


    @ViewBag.Title


    @RenderBody()

    So we need to reference Nancy. But how?

    By adding some xml to our web.config of course.

    1. <configSections>
    2.     <section name="razor" type="Nancy.ViewEngines.Razor.RazorConfigurationSection, Nancy.ViewEngines.Razor" />
    3.   </configSections>
    4.   <razor disableAutoIncludeModelNamespace="false">
    5.     <assemblies>
    6.       <add assembly="Nancy" />
    7.     </assemblies>
    8.     <namespaces>
    9.       <add namespace="Nancy.ViewEngines" />
    10.     </namespaces>
    11.   </razor>

    And now we get our nice login link.

    Or of course our nice logout link and username when we are logged in.

    Don't forget that if you have a test project to add an app.config file and add those same lines as above, else your tests will fail.

    Alas this is not enough. Our client doesn't want us to show the username of the logged in user but his or hers real name.

    But currentuser only returns us the usernmae. And why is that?
    Because currentuser is of type IUserIdentity and that only has two properties, namely: username and claims. And we used IUserIdentity when we made our AuthenticatedUser Class. So nancy is really returning us an AuthenticatedUser object and we can add stuff to that. Like the realname of the user.

    Like this.

    1. Imports Nancy.Security
    2.  
    3. Namespace Security
    4.  
    5.     Public Class AuthenticatedUser
    6.         Implements IUserIdentity
    7.  
    8.         Public Property UserName() As String Implements IUserIdentity.UserName
    9.         Public Property Claims() As IEnumerable(Of String) Implements IUserIdentity.Claims
    10.         Public Property RealName As String
    11.     End Class
    12. End Namespace

    Of course we now also have to update our userservice, usermodel, and the two user views to account for this added property.You can sse those changes in the code on github BTW.

    And now we can adapt our loginscript to show the realname instead of the username.

    1. @If Url.RenderContext.Context.CurrentUser Is Nothing Then
    2.         @<a href="/login">Login</a>
    3.     Else
    4.         @CType(Url.RenderContext.Context.CurrentUser, AuthenticatedUser).RealName  
    5.         @<a href="/logout">Logout</a>
    6.     End If

    I just cast currentuser to the type Authenticateduser and use it's realname.

    Of course this means I also need to add my assembly to the list of assemblies razor needs and add the namespace.

    Something like this.

    1. <razor disableAutoIncludeModelNamespace="false">
    2.     <assemblies>
    3.       <add assembly="Nancy" />
    4.       <add assembly="NancyDemo.VB" />
    5.     </assemblies>
    6.     <namespaces>
    7.       <add namespace="Nancy.ViewEngines" />
    8.       <add namespace="NancyDemo.VB.Security" />
    9.     </namespaces>
    10.   </razor>

    And now our view looks like this.

    And the sky is the limit again.

    Have fun.

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    nancy, vb.net
    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.)