我正在为我的silverlight应用程序实现自定义身份验证。如果这个代码在我的app.xaml中: -
<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 :(得分:2)
“有人”必须创建一个WebContext实例。如果从ApplicationLifeTimeObjects列表中删除它,则不会创建它。
以下是App构造函数中app.xaml.cs(实际上由“业务应用程序”Silverlight模板创建)中的等效代码:
WebContext webContext = new WebContext();
webContext.Authentication = new FormsAuthentication();
//webContext.Authentication = new WindowsAuthentication();
this.ApplicationLifetimeObjects.Add(webContext);