app.xaml / app.xaml.cs problem custom authentication
I am implementing custom a开发者_JS百科uthentication for my silverlight application. If this this code in my 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>
it works fine. However if i try to do the same thing in app.xaml.cs it doesn't work :-
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
WebContext.Current.Authentication = new FormsAuthentication();
}
It says WebContext.Current
threw an exception invalidoperationexception.
Thanks in advance :)
"Someone" has to create a WebContext instance. If you remove it from the list of ApplicationLifeTimeObjects, it will not be created.
Here is the equivalent code in app.xaml.cs (actually created by the "Business application" Silverlight template) in the App constructor:
WebContext webContext = new WebContext();
webContext.Authentication = new FormsAuthentication();
//webContext.Authentication = new WindowsAuthentication();
this.ApplicationLifetimeObjects.Add(webContext);
精彩评论