Securing WCF services using ASP.NET Auth and a DomainService with Silverlight
I have the basic ASP.NET authen开发者_如何学Pythontication domain service running in my Silverlight app (the one that comes with the VS2010 Silverlight Business template).
How can I use the authentication that this grants to secure methods exposed by standard WCF services (also hosted in the same app in IIS)?
Ok, so this is how you do it, the standard WCF service needs a couple of attributes, and you need to assign the Thread.CurrentPrincipal
:
[ServiceContract(Namespace = "")]
[SilverlightFaultBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
public Service1()
{
Thread.CurrentPrincipal = HttpContext.Current.User;
}
[OperationContract]
[PrincipalPermission(SecurityAction.Demand, Role = "Registered Users")]
public string DoSomeWork()
{
return "working";
}
}
A good place to start is here: Security for WCF RIA Services. What you're looking for is the RequiresAuthentication attribute.
[RequiresAuthentication]
public Foo SomeMethodCall(object parameter1)
{
return service.GetResult(parameter1)
}
精彩评论