Sharing authentication with ASP.NET and Silverlight
I have an ASP.NET web app that includes some silverlight pages. First, the users authenticate using a common ASP.NET webform.
This would be the typical code for that:
FormsAuthentication.SetAuthCookie(this.txtUsername.Text, false);
FormsAuthentication.RedirectFromLoginPage(this.txtUsername.Text, false);
Response.Redirect("~/Private/Index.aspx");
Next, the user navigates to a page that cointains a Silverlight page. From that silverlight page, I need to call some WCF services securely.
The service contains this code:
[ServiceContract(Namespace = "http://mydomain")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsM开发者_JS百科ode.Required)]
public class ServiceWCF
{
public ServiceWCF()
{
System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
Thread.CurrentPrincipal = HttpContext.Current.User;
}
[OperationContract]
[PrincipalPermission(SecurityAction.Demand, Authenticated = true)]
public List<Data> GetData()
{
// do things
}
}
And this is the way I call that method from the silverlight client:
MyWCFReference.ServiceWCFClient proxy = new MyWCFReference.ServiceWCFClient();
proxy.GetDataCompleted += new EventHandler<MyWCFReference.GetDataCompletedEventArgs>(proxy_GetDataCompleted);
proxy.GetDataAsync();
I followed this article to protect my webservices: http://netpl.blogspot.com/2010_04_01_archive.html
But when I run my code, I get the error: Request for principal permission failed.
Any idea about how to solve it? Thaaankkss!!
You probably need to set up a clientaccesspolicy.xml
or crossdomain.xml
file in the root of the domain where the WCF service is hosted to allow your Silverlight application access.
See this MSDN page for full details.
精彩评论