2013-12-19 45 views
1

如何使用FormsAuthentication概念來保護我的簡單WCF服務?WCF的表單身份驗證

ServiceContract類似於這樣:

[ServiceContract] 
    public interface MovieDb 
    { 
     [OperationContract] 
     string GetData(int value); 

     [OperationContract] 
     string Login(int value); 

     [OperationContract] 
     string Logout(int value); 

    } 

我在身份驗證和授權MVC 4應用程序使用FormsAuthentication

我能想到的就像在ServiceContract類的頂部添加Authorize過濾器屬性。

任何在簡單的指針非常讚賞的指針。謝謝。

回答

3

使用用戶名/密碼(Forms身份驗證),您可以保護您的WCF:

Message Security with a User Name Client

如果你決定使用會員對於服務器端的WFC配置中的身份驗證,您添加了配置成員身份的行爲:

<behavior name="myBehavior"> 
    <serviceAuthorization principalPermissionMode="UseAspNetRoles" roleProviderName="myRoleProvider"/> 
    <serviceCredentials> 
     <serviceCertificate findValue="*.mycert.net" storeLocation="LocalMachine" x509FindType="FindBySubjectName"/> 
     <userNameAuthentication userNamePasswordValidationMode="MembershipProvider" membershipProviderName="myMembershipProvidewr"/> 
    </serviceCredentials> 
</behavior> 

你的WCF可以驗證一樣簡單

[PrincipalPermission(SecurityAction.Demand, Role = "My Role")] 
     public bool GetSomething(string param1) 
     { 
... 

您可以在這裏找到更多的信息: http://msdn.microsoft.com/en-us/library/ff650067.aspx

1

我知道MVC中非常有用的Authorize關鍵字,但是我沒有在WCF中找到類似的東西。

如果您在(和for)在同一個IIS應用程序中使用WCF服務,則可能需要編寫一個自己的Authorize關鍵字實現,但對於WCF來說則是如此。您可以參考HttpContext查看請求是否被自動化。

一些額外的信息可以在這裏找到:

Does WCF have an equivalent of MVC's [Authorize] attribute?