Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Silverlight Windows User Identity Name

0.00/5 (No votes)
15 Dec 2009 1  
How to get the Windows user identity name in Silverlight.

Introduction

This article explains how to get the Window User Identity Name in Silverlight without using WCF.

Background

I was looking for Windows Authentication for Silverlight on the web. After Binging and Googling for a while, I found most of them use a WCF service to do the Windows authentication, where the primary objective is to just get the current user name. I thought using WCF for just getting the Windows user is kind of overkill, and moreover, you have to configure your basicHttpBinding to incorporate Windows authentication with WCF, which is kind of trivial, but can turn painful if not configured properly.

Below are three quick and easy steps to achieve the objective:

  1. Open the ASPX page that carries the Silverlight XAP file inside the Object tag and add a new param (let's call it Initparams):
  2. <param name="Initparams" 
      value="UserAccount=<%=HttpContext.Current.User.Identity.Name%>" />
  3. Open the App.xaml.cs file and declare a global variable:
  4. public static string UserID = string.Empty;

    To the application_startup method in App.xaml.cs, assign the param value to the global variable (this should be before the RootVisual statement):

    UserID = e.InitParams["UserAccount"];
  5. Declare a variable in your Mainpage.xaml or the navigation page, and assign the global variable value to the local variable, and you have the current logged on Windows user name.
  6. string UserAccount = App.UserID;

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here