4

I have seen this question posed regarding Silverlight 2 but I have not seen any questions about Silverlight 4.

Is there a way to get the current user running an application in Silverlight 4.0? I thought I remember seeing that as one of the features of 4.0 but I cannot find it. (Maybe it was just wishful thinking on my part.) I would imagine it would have to come from the OS running the browser or the browser itself. (The application is not being installed locally, it's running in the browser)

I have a solution where I call web service method that just does the following to get the users name. However, I would like to not have to call a web service

HttpContext.Current.User.Identity.Name.ToUpper();

Thanks in advance!

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
hewstone
  • 4,425
  • 2
  • 23
  • 24

4 Answers4

2

Getting the Username client-side in standard Inbrowser silverlight app just isn't going to happen, it would be a really bad thing security wise if it were possible.

Personally if I had this requirement I would probably use some web service, WCF or a simple IHttpHandler server-side. However I would be in inclined to call it something like "UserContext" and send XML. The XML would contain the Username. This would allow for additional user specific state to be added to the XML later as further requirements become clear. Adding these new chunks of info would be easy.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
1

In your Silverlight object tag add a new param with name as initParam (note the casing). Through this you can pass values as key=value[,key=value,...] pairs. Here I am passing a key of usrIdentity and value as the Current User Identity Name.

<param name="initParams" value='usrIdentity=@HttpContext.Current.User.Identity.Name'/>

In your App.xaml.cs in the Application_StartUp you would have the following code

private void Application_Startup(object sender, StartupEventArgs e)
{
    this.RootVisual = new MainPage();
    if (e.InitParams.ContainsKey("usrIdentity"))
    {
        //I have a public static string WindowsUser on my MainPage.xaml.cs
        MainPage.WindowsUser = e.InitParams["usrIdentity"];
    }
}

This is from hewstone's suggestion. The MS link is http://msdn.microsoft.com/en-us/library/cc189004(v=vs.95).aspx

Vishnoo Rath
  • 550
  • 7
  • 22
1

You might find it easier just to pass the name into the silverlight app as a parameter

mcintyre321
  • 12,996
  • 8
  • 66
  • 103
  • if you can dynamically change the page context (as in an aspx or mvc view) http://nerddawg.blogspot.com/2008/03/how-to-pass-initialization-params-to.html instead of startPage=Page1 write username=<%= User.Identity.Name %> – mcintyre321 Aug 12 '10 at 19:23
  • if you really need to, have Silverlight interrogate the current url for the username – mcintyre321 Aug 12 '10 at 19:24
  • Sorry for the late reply. The simple solution is to just turn on pass through authentication for your IIS site. Then in the default.aspx (or what asp page launches your application) to put the user in the InitParams like this and read them in the mainpage.xaml.cs: `System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append(""); InitParams.Text = sb.ToString()` – hewstone Jun 09 '11 at 13:19
0

Correction to @Vishnoo Rath; you should try this:

<param name="initParams" 
       value="usrIdentity=<%=HttpContext.Current.User.Identity.Name%>"/>
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116