Windows Authentication in Silverlight


In this code snippet, I will shows you how to get the logged in user's username and pass it the Silverlight application using Javascript.

The Windows Authentication can be implemented in two ways, First is by making a WCF service call and second is using JavaScript.

The First method, with detailed steps is described at the below URL

http://rouslan.com/2009/03/20-steps-to-get-together-windows-authentication-silverlight-and-wcf-service/

The second method (which I feel is mush better/faster than first) is described below.

Steps:

1.) First of all, create a hidden field on the aspx page (that is hosting the Silverlight application) call it "hdnUsername"


<asp:HiddenField ID="hdnUsername" runat="server" />


2.) In the Page_Load() get the username from "User.Identity.Name" and assign it to the hidden field.


if (User != null && User.Identity != null && User.Identity.IsAuthenticated)
{
string username = User.Identity.Name.ToString().Substring(User.Identity.Name.ToString().IndexOf('\\') + 1).ToLower();
hdnUsername.Value = username;
}


User.Identity.Name gives the username with domain for example, Domain\Username, so to get the username I have to split the string and take whatever is after '\'

3.) Now create Javascript function on the aspx page as follows,

<script type="text/javascript" defer="defer">

function setUserName() {
var hdnUsername = document.getElementById("hdnUsername");
var slControl = document.getElementById("silverlightControl"); //silverlightControl is the id of object tag of SL app
slControl.Content.Test.SetUsername(hdnUsername.value);
}

</script>


what this function does is, it reads the username from the hidden field and passes it to the silverlight application by calling the SetUsername() method of Silverlight application.

4.) Now in the MainPage.xaml.cs file (or whatever is the first page name of your Silverlight application) create a function Initialize(); and call it right after InitializeComponent();


Initialize()
{
HtmlPage.RegisterScriptableObject("Test", this); // register the app so that JavaScript function can talk to your SL app
HtmlPage.Window.Invoke("setUserName", string.Empty); // Call to the Javascript function
}


5.) Finally, create SetUsername() method.


public void SetUsername(string username)
{
//Do what ever you want with the username
}


Done. Now run the app and test.

6.) One addition can be made to the above code, Suppose if JavaScript is disabled in the user's browser so this method will fail. So, we can modify the Initialize() a bit to take care of this scenario.


Initialize()
{
try
{
HtmlPage.RegisterScriptableObject("Test", this); // register the app so that JavaScript function can talk to your SL app
HtmlPage.Window.Invoke("setUserName", string.Empty); // Call to the Javascript function
}
catch
{
// Call the WCF service to get the Windows credentials of the user as described in the first method

}
}


What we are doing here is, first we make a call to the Javascript method. If the user has Javascript disabled then this call will fail. So we are catching this exception and then we make a WCF service call to get the logged in username.

Hope this helps. Keep Coding !!!


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: