2010-11-11 47 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);