WCF Ria Services - Active Directory Roles
January 26, 2010 at 9:02 PM
—
dsoltesz
I ran into a issue using wcf ria services with windows authentication and getting the currently logged in users roles. I followed the steps to enable windows authentication.
1. Change App.xaml to use Windows Authentication (By default the Silverlight Business Application template has Forms Authentication)
<Application.ApplicationLifetimeObjects>
<app:RiaContext>
<app:RiaContext.Authentication>
<!--<appsvc:FormsAuthentication/>-->
<appsvc:WindowsAuthentication/>
</app:RiaContext.Authentication>
</app:RiaContext>
</Application.ApplicationLifetimeObjects>
2. Change Web.config to use Windows Authentication
<!--The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
3. Load current user. In your app.xaml.cs
// This will automatically authenticate a user when using windows authentication
RiaContext.Current.Authentication.LoadUser(this.Application_UserLoaded, null);
After the user loaded I then wanted to access the users AD roles.
/// <summary>
/// Invoked when the <see cref="LoadUserOperation"/> completes. Use this
/// event handler to switch from the "loading UI" you created in
/// <see cref="InitializeRootVisual"/> to the "application UI"
/// </summary>
private void Application_UserLoaded(LoadUserOperation operation)
{
foreach (var r in RiaContext.Current.User.Roles)
{
//do something
}
}
However, RiaContext.Current.User.Roles.Count was always zero
I had missed a key step in my configuration that tells your app which role provider to use.
4. Setup windows role provider in web.config
<roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider"/>
After adding this setting to my web.config, all of my AD roles now appeared in RiaContext.Current.User.Roles