9-10 2010

Setting the RIA AuthenticationService DomainContextType

 RIA Services provides a Authentication Domain Service template to use for enabling windows or forms authentication in your Silverlight application.  Most of the out-of-the-box setup works fine if your project has everything in a single assembly, but when building larger scale applications that may use a RIA Services Class Library, there are a few extra steps you'll need to take for everything to work correctly.

You'll know there is a problem finding your AuthenticationContext if you get this error:

The DomainContextType is null or invalid and there are no contexts generated from AuthenticationBase<T> 

If you are setting up your WebContext (or whatever you may have named your RIA Context that derives from WebContextBase) in App.xaml, the extra code is a bit more intuitive.  Simply set the DomainContextType so it points to the assembly where your generated AuthenticationContext is located:

<Application.ApplicationLifetimeObjects>
    <svc:WebContext>
        <svc:WebContext.Authentication>
            <appsvc:FormsAuthentication DomainContextType="My.RIA.Namespace.AuthenticationContext, My.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
            <!--<appsvc:WindowsAuthentication/>-->
        </svc:WebContext.Authentication>
    </svc:WebContext>

</Application.ApplicationLifetimeObjects>

 

If you are declaring your WebContext in the codebehind of App.xaml.cs, you need to set the DomainContext like this:


using System.ServiceModel.DomainServices.Client.ApplicationServices;
//...
public App()
{
    this.Startup += this.Application_Startup;
    this.Exit += this.Application_Exit;
    this.UnhandledException += this.Application_UnhandledException;
  
    InitializeComponent();
  
    // Create a WebContext and add it to the ApplicationLifetimeObjects
    // collection.  This will then be available as WebContext.Current.
    WebContext webContext = new WebContext();
    webContext.Authentication = new FormsAuthentication();
    //webContext.Authentication = new WindowsAuthentication();
    this.ApplicationLifetimeObjects.Add(webContext);
}
private void Application_Startup(object sender, StartupEventArgs e)
{
    // AuthenticationContext exists in a separate assembly, reference it here
    ((WebAuthenticationService)WebContext.Current.Authentication).DomainContext = new My.RIA.Namespace.AuthenticationContext();
              
    // This will enable you to bind controls in XAML files to WebContext.Current
    // properties
    this.Resources.Add("WebContext", WebContext.Current);
  
    // This will automatically authenticate a user when using windows authentication
    // or when the user chose "Keep me signed in" on a previous login attempt
    WebContext.Current.Authentication.LoadUser(this.Application_UserLoaded, null);
  
    // Show some UI to the user while LoadUser is in progress
    this.InitializeRootVisual();
}

4 Comments

  1. 1 Mike 18 Oct
    Josh,

    Great post!  I am currently working with your Enhanced Silverlight Business Application Template.  The template has been great and has provided such a headstart in our understanding of best practices in SL development.  I am not trying to the authentication from Forms to Windows and I'm not quite sure how best to accomplish this task.  In trying to figure out how to make this change within in your template, I keep receiving the following exception:

    The DomainContextType is null or invalid and there are no contexts generated from AuthenticationBase<T> 

    Could you please let us know how best to convert your template from Forms Authentication to Windows Authentication?

    Thank you in advance.

    Mike
  2. 2 Lee Eastaff 20 Jun
    Thank you so much , I been trying to fix this issue for 2 days now , Great post
  3. 3 Waleed 07 Jul
    Dear Josh,

    I am not using your templete, but the same approach for the authentication.
    I have a problem when I set isPersistent isI receive the following error from the Application_Startup "Load operation failed for query GetUser(), entity user : null can't be added to cache ..."

    Do you know why I get this error ...

    Best regards
  4. 4 Renaldo 03 Jan
    thanks for the help,this fixed my problem

Comment

  1.