Step by Step Customizing RD Web Access 2012 R2 – Part 5


Adding the current user’s Active Directory displayname to RD Web Access 2012R2

 

This is another addition to the “Customize RD Web Access 2012 R2” series.

This post is inspired by comments in the series, in which readers asked if it was possible to show the logged in user’s displayname as it appears in Active Direcory.
I did a little digging in the code and found a way to accomplish this.

I’m not going to detail the setup I have here. If you’re interested I suggest you read up on the series.

Logging into the RD Web Access shows this:
RDS Customize Web Access - show user displayname 01
This is actually the top of the Default.aspx page.
Just basic stuff here, nothing customized, it shows a basic navigation bar. I’m going to show you how you can add the displayname right after the “Sign out” button on the navigation bar.

The files that make up RD Web Access are in C:\Windows\Web\RDWeb\Pages so make a copy of that folder structure just to make sure we have a backup should we break something.

We are going to use native Windows code from an assembly that is already present on the server that holds the RD Web Access role, but is not known to the RD Web Access application yet. So the first step is to tell the application that we are going to add code.

Open web.config from C:\Windows\Web\RDWeb\Pages in an editor.
Find line 52:
RDS Customize Web Access - show user displayname 02
And add the following code right after that:

<compilation defaultLanguage="c#" debug="true">
  <assemblies>
    <add assembly="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
  </assemblies>
</compilation>

The result must look like this:
RDS Customize Web Access - show user displayname 03

This tells the RD Web Access application that we need extra code. The extra code is in the .NET Assembly, and specifically in System.DirectoryServices.dll.
Save and close web.config, we don’t need it anymore.

Now open Default.aspx in an editor, which is in C:\Windows\Web\RDWeb\Pages\en-us.
Find line 225:
RDS Customize Web Access - show user displayname 04

Insert the following code before line 225:

private static string GetDisplayName(string strUserName)
{
 string strLDAPPath = "LDAP://dc=itw,dc=test";
 string strFilter = string.Empty;

 if(strUserName.Contains("\\")){strUserName = strUserName.Substring(1 + strUserName.IndexOf("\\"));}
 strFilter = "(SAMAccountName=" + strUserName + ")";
 if(strUserName.Contains("@")){strFilter = "(UserPrincipalName=" + strUserName + ")";}
 
 System.DirectoryServices.DirectoryEntry de = new System.DirectoryServices.DirectoryEntry(strLDAPPath);
 System.DirectoryServices.DirectorySearcher ds = new System.DirectoryServices.DirectorySearcher(de);
 ds.Filter = strFilter;
 ds.PropertiesToLoad.Add("DisplayName");
 System.DirectoryServices.SearchResultCollection results = ds.FindAll();

 return (results != null && results.Count > 0) ? results[0].Properties["DisplayName"][0].ToString() : string.Empty;
}

Remember to replace “LDAP://dc=itw,dc=test” with your own domain name. So for example, if your domain name is “contoso.local”, this code would be “LDAP://dc=contoso,dc=local”.
The result must look like this (I have wordwrap on in my editor):
RDS Customize Web Access - show user displayname 07

This shows line 225-242 in my file.

Now find line 249:
RDS Customize Web Access - show user displayname 06

And add the following line of code right after that:

userdisplayname="<%=GetDisplayName(strDomainUserName)%>"

The result must look like this:
RDS Customize Web Access - show user displayname 11

This last modification tells the xsl template that it can expect a new variable, and it’s called “userdisplayname”. The value of that variable is the result of the GetDisplayName code you inserted earlier.
Save and close Default.aspx.

Note that if you want to show the displayname on other pages than Default.aspx you need to make these two changes in those files as well! The modifications above in Default.aspx will only work on Default.aspx.

Now open Site.xsl in an editor, which is in C:\Windows\Web\RDWeb\Pages.

Find line 15 (empty line) and insert the following code:

<xsl:variable name="userdisplayname" select="/RDWAPage/@userdisplayname"/>

The result will look like this:
RDS Customize Web Access - show user displayname 08
Here we grab the new variable which we defined in Default.aspx (and now you see why we need to do this on every page that we want to show the displayname on, because the variable is supplied by the actual page).

Find line 323:
RDS Customize Web Access - show user displayname 09

Add the following code on the end of that line:

<xsl:if test="$userdisplayname"> (<xsl:value-of select="$userdisplayname"/>)</xsl:if>

The result must look like this:
RDS Customize Web Access - show user displayname 10

Save and close Site.xsl.

Open your RD Web Access page, login using valid credentials:
RDS Customize Web Access - show user displayname 12

Nice! We now have the displayname displayed on the navigation bar, right next to the Sign out button.
I have tested this in my very simple lab setup. I have only one domain, with a single UPN context, so I couldn’t test this with aliases and such.
I have tested this customization using DOMAIN\username, UPN and just username, and that all works.

About thangletoan

Hallo Aloha

Posted on 13/10/2012, in RD Web App, Remote App, Remote Assistant, Remote Desktop, Remote Desktop Host Server, RemoteFX and tagged , , . Bookmark the permalink. Để lại bình luận.

Gửi phản hồi

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Log Out / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Log Out / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Log Out / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Log Out / Thay đổi )

Connecting to %s

%d bloggers like this: