Silverlight Deployed To SharePoint With Active Directory

This code snippet offers a simplistic way to obtain the Active Directory credentials of a user accessing a Silverlight application hosted in a SharePoint page.

In order to actually determine Active Directory roles or obtain additional data on the user in Active Directory, you would typically write a WCF service that accepts an Active Directory username and whatever search credentials you want. The service would return back additional Active Directory details. This sample is just a quick and dirty way of retrieving the user's Active Directory username when they try to access the folder the Silverlight application is hosted in. In most cases, the user would have authenticated via Active Directory while logging into SharePoint. Those credentials are automatically passed when they attempt to load your Silverlight application hosted in the SharePoint Page.

Our GetActiveDirectoryUserName.aspx will return an empty string if your Silverlight web site does not require windows authentication. So, make sure you disable anonymous authentication.

You can also accomplish this task by using a SharePoint WebPart that loads the part via a url. In that instance, you could avoid the WebClient request and instead put your Silverlight object tags in an aspx page. The aspx page could register some JavaScript function at start up with the REMOTE_USER credentials written to it. From there, Silverlight can call the JavaScript method to load the value.

// Typical Page.xaml.cs

using System;
using System.Net;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;

namespace SilverlightApplication1
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
LoadActiveDirectoryUserName();
}

public void LoadActiveDirectoryUserName()
{
// write your own code to populate the proper domain based on local dev,
// qa/staging, or production environments.

// Use a simple ASP.NET page to reference the Request.ServerVariables collection
// server side if you don't already have a WCF service that requires matching
// ActiveDirectory credentials.

string url = "http://localhost:43235/GetActiveDirectoryUserName.aspx";

this.ActiveDirectoryUserName.Text = "Looking for user...";
var client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(OnGetActiveDirectoryUserName_Completed);
client.DownloadStringAsync(new Uri(url, UriKind.Absolute));
}

void OnGetActiveDirectoryUserName_Completed(object sender, DownloadStringCompletedEventArgs e)
{
string activeDirectoryUserName = e.Result;
this.ActiveDirectoryUserName.Text = "Active Directory User: " + activeDirectoryUserName;
// Fire of user interface processes that required authentication to ActiveDirectory.
}

}
}
// GetActiveDirectoryUserName.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<% Response.Write(Request.ServerVariables["REMOTE_USER"]);%>
// SilverlightHtml.js

function LoadSilverlightToPage()
{
document.write('<style type="text/css"> #silverlightControlHost { height: 100%; } </style>');
document.write('<div id="errorLocation" style="font-size: small;color: Gray;"></div>');
document.write('<div id="silverlightControlHost">');
document.write('<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">');
document.write('<param name="source" value="ClientBin/SilverlightApplication1.xap"/>');
document.write('<param name="onerror" value="onSilverlightError" />');
document.write('<param name="background" value="white" />');
document.write('<param name="minRuntimeVersion" value="2.0.31005.0" />');
document.write('<param name="autoUpgrade" value="true" />');
document.write('<a href="http://go.microsoft.com/fwlink/?LinkID=124807"
temp_href=http://go.microsoft.com/fwlink/?LinkID=124807 style="text-decoration: none;">'
);
document.write(' <img src="http://go.microsoft.com/fwlink/?LinkId=108181"
temp_src=http://go.microsoft.com/fwlink/?LinkId=108181 alt="Get Microsoft Silverlight" style="border-style: none"/>'
);
document.write('</a>');
document.write('</object>');
document.write('</div>');
}
LoadSilverlightToPage();
// Test2.htm. Drop the javascript lines into a SharePoint page with the same primary domain name as the Silverlight is hosted under.

<html>
<body>
<script type="text/javascript" src="Silverlight.js"></script>
<script type="text/javascript" src="SilverlightHtml.js"></script>
</body>
</html>
By Robbe Morris   Popularity  (3418 Views)
Picture
Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of NullSkull.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.  Robbe also loves to scuba dive and go deep sea fishing in the Florida Keys or off the coast of Daytona Beach. Microsoft MVP