Skip to main content
 首页 » 编程设计

c#之WCF 的表单例份验证

2024年02月13日13tintown

如何使用 FormsAuthentication 概念保护我的简单 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 Filter 属性。

非常感谢任何简单的指示。谢谢。

请您参考如下方法:

您可以使用用户名/密码(表单例份验证)保护您的 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