Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#

Silverlight Windows User Identity Name

Rate me:
Please Sign up or sign in to vote.
5.00/5 (14 votes)
15 Dec 2009CPOL1 min read 104.9K   1.2K   23   30
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. XML
    <param name="Initparams" 
      value="UserAccount=<%=HttpContext.Current.User.Identity.Name%>" />
  3. Open the App.xaml.cs file and declare a global variable:
  4. C#
    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):

    C#
    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. C#
    string UserAccount = App.UserID;

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
South Africa South Africa
Sivakumar Ramakrishnan

Interest : Programming, Reading, Music, Blogging

Comments and Discussions

 
Questione.InitParams.Count = 0 Pin
chrisndirangu27-Sep-12 3:49
chrisndirangu27-Sep-12 3:49 
QuestionNot working within SharePoint environment Pin
Khaniya5-Sep-11 18:20
professionalKhaniya5-Sep-11 18:20 
QuestionDoes not work when hosted on IIS Pin
nilesh.dt20-Apr-11 2:16
nilesh.dt20-Apr-11 2:16 
AnswerRe: Does not work when hosted on IIS Pin
BrentLak17-Jan-12 3:53
BrentLak17-Jan-12 3:53 
GeneralRe: Does not work when hosted on IIS Pin
Member 38698188-Aug-12 1:05
Member 38698188-Aug-12 1:05 
GeneralRe: Does not work when hosted on IIS Pin
Member 965682710-Jul-13 8:21
Member 965682710-Jul-13 8:21 
GeneralMy vote of 5 Pin
Marcio_Coelho15-Mar-11 12:23
Marcio_Coelho15-Mar-11 12:23 
GeneralMy vote of 5 Pin
edgardanielcc14-Jan-11 12:24
edgardanielcc14-Jan-11 12:24 
QuestionCan be hacked Pin
douglampe4-Aug-10 3:35
douglampe4-Aug-10 3:35 
GeneralMy vote of 5 Pin
Brad McBride8-Jul-10 10:07
Brad McBride8-Jul-10 10:07 
GeneralUser Machine Name Pin
rahulreddysandadi23-Jun-10 5:52
rahulreddysandadi23-Jun-10 5:52 
GeneralNot successful... Pin
clarkyip25-Apr-10 16:15
clarkyip25-Apr-10 16:15 
GeneralRe: Not successful... Pin
Webplethora25-Apr-10 19:29
Webplethora25-Apr-10 19:29 
GeneralRe: Not successful... Pin
clarkyip5-May-10 23:58
clarkyip5-May-10 23:58 
GeneralNice! Pin
madeinjapan10-Mar-10 3:41
madeinjapan10-Mar-10 3:41 
GeneralWorks for me Pin
jackmos4-Mar-10 5:16
professionaljackmos4-Mar-10 5:16 
GeneralI am getting error on running the project Pin
jay.407.net26-Jan-10 0:02
jay.407.net26-Jan-10 0:02 
GeneralRe: I am getting error on running the project Pin
Webplethora26-Jan-10 20:28
Webplethora26-Jan-10 20:28 
GeneralRe: I am getting error on running the project Pin
nyssan5-Oct-10 1:11
nyssan5-Oct-10 1:11 
GeneralGreat Pin
robalexclark14-Jan-10 23:03
robalexclark14-Jan-10 23:03 
GeneralRe: Great Pin
Webplethora15-Jan-10 4:30
Webplethora15-Jan-10 4:30 
Thank you.
GeneralWell... Pin
Dewey15-Dec-09 23:01
Dewey15-Dec-09 23:01 
GeneralRe: Well... Pin
Webplethora16-Dec-09 20:44
Webplethora16-Dec-09 20:44 
Generalthis is not article Pin
Artem S. Dmitriev15-Dec-09 7:53
Artem S. Dmitriev15-Dec-09 7:53 
GeneralRe: this is not article Pin
Morten Holk Maate15-Dec-09 21:04
Morten Holk Maate15-Dec-09 21:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.