2010-11-11 14 views
1

J'implémente l'authentification personnalisée pour mon application Silverlight. Si ce code dans mon app.xaml: -app.xaml/app.xaml.cs problème authentification personnalisée

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      x:Class="SilverlightCustomAuthExample.App" 
      xmlns:local="clr-namespace:SilverlightCustomAuthExample" 
      xmlns:appsvc="clr-namespace:System.ServiceModel.DomainServices.Client.ApplicationServices;assembly=System.ServiceModel.DomainServices.Client.Web" 
      > 
    <Application.Resources> 

    </Application.Resources> 
    <Application.ApplicationLifetimeObjects> 
     <local:WebContext> 
      <local:WebContext.Authentication> 
       <appsvc:FormsAuthentication/> 
      </local:WebContext.Authentication> 
     </local:WebContext> 
    </Application.ApplicationLifetimeObjects> 
</Application> 

cela fonctionne très bien. Toutefois, si je tente de faire la même chose dans App.xaml.cs il ne fonctionne pas: -

private void Application_Startup(object sender, StartupEventArgs e) 
     { 
      this.RootVisual = new MainPage(); 
      WebContext.Current.Authentication = new FormsAuthentication(); 


     } 

Il dit WebContext.Current jeté un InvalidOperationException d'exception.

Merci à l'avance :)

+0

Quelle est votre nouvelle app.xaml (dans le cas où il se bloque)? – Timores

Répondre

2

"Quelqu'un" doit créer une instance WebContext. Si vous le supprimez de la liste des ApplicationLifeTimeObjects, il ne sera pas créé.

Voici le code équivalent en App.xaml.cs (en fait créé par le modèle Silverlight « application Business ») dans le constructeur App:

 WebContext webContext = new WebContext(); 
     webContext.Authentication = new FormsAuthentication(); 
     //webContext.Authentication = new WindowsAuthentication(); 
     this.ApplicationLifetimeObjects.Add(webContext);