You are here:   Blog
Register   |  Login

Dec 16

Written by: Michael Washington
12/16/2012 9:51 AM  RssIcon

image

In the Visual Studio LightSwitch HTML Client, extra steps are required to determine who the currently logged in user is.

image

The first step is to turn on authentication in the LightSwitch project.

image

We create a UserName field. Notice that it is Required. This is the thing that will cause a problem (demonstrated later).

image

We select the Entity (table), then we select Write Code, then the Inserting method.

We use the following code for the method:

 

        partial void PromiseOrdersSet_Inserting(PromiseOrders entity)
        {
            // Set the Username 
            entity.UserName = this.Application.User.Name;
        }

 

We also set the updating method:

 

        partial void PromiseOrdersSet_Updating(PromiseOrders entity)
        {
            // Set the Username 
            entity.UserName = this.Application.User.Name;
        }

 

We need to do this to protect the OData service points. Only setting this on the client side code (shown later) is not enough.

A user could access the OData service point directly and alter the value. Using the code above prevents this.

image

However, the UserName is not populated when we run the application.

image

To fix this, we first switch to File View.

image

We add a page to the Server project using the following code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LightSwitchApplication.Web
{
    public class GetUserName : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            using (var serverContext = ServerApplicationContext.CreateContext())
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write(serverContext.Application.User.Name);
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

(this code uses the new serverContext API)

 

image

We switch back to Logical View, select the Entity, Client (tab), Write Code, and then the created method.

We add the following code:

 

myapp.PromiseOrders.created = function (entity) {
    // Set the default date for the Order
    entity.OrderDate = new Date();
    // Using a Promise object we can call the CallGetUserName function
    msls.promiseOperation(CallGetUserName).then(function PromiseSuccess(PromiseResult) {
        // Set the result of the CallGetUserName function to the 
        // UserName of the entity
        entity.UserName = PromiseResult;
    });
};
// This function will be wrapped in a Promise object
function CallGetUserName(operation) {
    $.ajax({
        type: 'post',
        data: {},
        url: '../web/GetUserName.ashx',
        success: operation.code(function AjaxSuccess(AjaxResult) {
            operation.complete(AjaxResult);
        })
    });
}

 

We run the project:

image

The UserName is now retrieved.

 

LightSwitch Help Website Articles

Writing JavaScript That Implements The Binding Pattern In Visual Studio LightSwitch

Implementing The Wijmo Radial Gauge In The LightSwitch HTML Client

Writing JavaScript In LightSwitch HTML Client Preview

Creating JavaScript Using TypeScript in Visual Studio LightSwitch

Theming Your LightSwitch Website Using JQuery ThemeRoller

Using Toastr with Visual Studio LightSwitch HTML Client (Preview)

 

LightSwitch Team HTML and JavaScript Articles

Custom Controls and Data Binding in the LightSwitch HTML Client (Joe Binder)

Creating Screens with the LightSwitch HTML Client (Joe Binder)

The LightSwitch HTML Client: An Architectural Overview (Stephen Provine)

Writing JavaScript Code in LightSwitch (Joe Binder)

New LightSwitch HTML Client APIs (Stephen Provine)

A New API for LightSwitch Server Interaction: The ServerApplicationContext

Building a LightSwitch HTML Client: eBay Daily Deals (Andy Kung)

 

Download Code

The LightSwitch project is available at http://lightswitchhelpwebsite.com/Downloads.aspx

(you must have HTML Client Preview 2 or higher installed to run the code)

11 comment(s) so far...


Gravatar

Re: Retrieving The Current User In The LightSwitch HTML Client

Hi,

I'm try write down the stps above but I'm have problem with the part "We switch back to Logical View, select the Entity, Client (tab), Write Code, and then the created method.". I can't find the Client (tab). Beside that I can't find the "created method" (I understand because of the missing tab, right ?

Thanks for the help. Congratulations for the blog (Good job).

By Ventocilla on   1/9/2013 4:50 PM
Gravatar

Re: Retrieving The Current User In The LightSwitch HTML Client

@Ventocilla - If you do not see that Client Tab then you do not have the HTML Client Preview 2 installed. There is a link at the bottom of my Blog post.

By Michael Washington on   1/9/2013 4:58 PM
Gravatar

Re: Retrieving The Current User In The LightSwitch HTML Client

Ok. Thanks a lot. I'm going to check that.

By Ventocilla on   1/12/2013 3:41 PM
Gravatar

Re: Retrieving The Current User In The LightSwitch HTML Client

Hi Michael,
thanks for the article, but i'm having some problems with this when I try to deploy it to an azure website. When running in debug mode all is well but when I try to run deployed on azure website it doesn't work.

To test this I added a simple Desktop client to your sample code just so that I could get the Administration screens to add new users. I added two users; test1 and test2, using the desktop client.

When I run the HTMLClient it prompts me for username and password as expected, but when I make the ajax call I receive an empty string for the user. I've tested that the IHttpHandler code is being called by hardcoding a dummy username to be returned.

Any idea what i'm doing wrong here!!! Any help would be greatly appreciated.

By dparker on   1/22/2013 4:17 AM
Gravatar

Re: Retrieving The Current User In The LightSwitch HTML Client

@dparker - I suspect that the path that the JavaScript is using needs to be adjusted when you deploy to Azure. Using Fiddler should help you figure out what is wrong.

By Michael Washington on   1/22/2013 5:49 AM
Gravatar

Re: Retrieving The Current User In The LightSwitch HTML Client

@dparker - The previous Beta of LightSwitch did not properly work with Forms Authentication. The new version does so this should no longer be a problem.

By Michael Washington on   3/14/2013 6:11 AM
Gravatar

Retrieving The Current User In The LightSwitch HTML Client

Hi michael washington , i deployed the PictureUploader application . can you give login user name and password , i try the server authentication its throw the this error "Your login attempt was not successful. Please try again"

By venkadesan on   4/18/2013 7:41 AM
Gravatar

Re: Retrieving The Current User In The LightSwitch HTML Client

@venkadesan - When you deploy an application the Publish wizard has a screen that allows you to set the username and password.

By Michael Washington on   4/18/2013 8:56 AM
Gravatar

Re: Retrieving The Current User In The LightSwitch HTML Client

Michael,
Does this work for windows authentication as well or only for forms authentication?
Scott

By Scott on   4/25/2013 9:44 AM
Gravatar

Re: Retrieving The Current User In The LightSwitch HTML Client

@Scott - It has only been tested with Forms Authentication so I do not know.

By Michael Washington on   4/25/2013 9:45 AM
Gravatar

Re: Retrieving The Current User In The LightSwitch HTML Client

Michael,
I had the code in the Entity "Created" js instead of the html client browseappointments "created" screen. It works with AD/Windows and returns the correct username. :)
Scott

By Scott on   4/25/2013 2:09 PM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 

Microsoft Visual Studio is a registered trademark of Microsoft Corporation / LightSwitch is a registered trademark of Microsoft Corporation