2010-11-11 51 views
1

我正在爲我的Silverlight應用程序實現自定義身份驗證。如果這個代碼在我的app.xaml中: -app.xaml/app.xaml.cs問題自定義身份驗證

<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> 

它工作正常。但是,如果我嘗試做同樣的事情在app.xaml.cs它不工作: -

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


     } 

它說WebContext.Current拋出異常InvalidOperationException異常。

在此先感謝:)

+0

什麼是新的App.xaml(的情況下它崩潰)? – Timores 2010-11-11 12:18:33

回答

2

「某人」必須創建一個WebContext實例。如果您從ApplicationLifeTimeObjects列表中刪除它,它將不會被創建。

這是在App構造函數中app.xaml.cs的等效代碼(實際上是由「商務應用」的Silverlight模板創建):

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