WCF,ASP.NET兼容模式和使用成员资格提供程序的自定义验证

时间:2010-10-26 12:07:20

标签: asp.net authentication cookies authorization wcf-security

我需要以下方面的帮助:)

首先,我处理具有WinForms客户端和服务器的大型应用程序。在我们的例子中,服务器是一组WCF服务。有一项服务负责用户身份验证。身份验证的逻辑是自定义和复杂的,身份验证服务使用不同的成员资格提供程序。

我们希望保护对未经身份验证的用户的服务器服务的访问。用户必须首先进行身份验证,然后使用其他服务(在这种情况下,用户是其他系统,服务,WinForms客户端等)。在此基础上,我们决定使用ASP.NET Url / File Authorization功能。

因此,我设置了ASP.NET兼容模式,允许所有绑定配置中的cookie,将AspNetCompatibilityRequirements属性添加到我们的服务中,并将以下配置添加到config:

    <authentication mode="Forms">
      <forms cookieless="UseCookies">
        <credentials passwordFormat="Clear" />
      </forms>
    </authentication>

    <authorization>
       <deny users="?" />
    </authorization>

    ...

<location path="AuthenticationService.svc">
    <system.web>
        <authorization>
            <allow users="*" />
        </authorization>
    </system.web>
</location>

在我们的身份验证服务的身份验证方法中,我添加以下代码:

public AuthenticationResult AuthenticateUser(string username, string password)
{
    AuthenticationResult result = new AuthenticationResult();
    result = Authenticate(username, password);

    if (result.IsAuthenticated)
        FormsAuthentication.SetAuthCookie(username, true);

    return result;
}

接下来,我编写了以下代码:

var authClient = new AuthenticationServiceClient();
var result = authClient.AuthenticateUser("user", "password");
var otherClient = new OtherServiceClient();
var temp = otherClient.DoSomething();    

但是在身份验证后我无法访问OtherServiceClient ...

那么,我如何在WCF服务调用之间共享调用上下文?有人可以提供一些关于这个问题的有用文章吗?

提前致谢! 最好的问候。

1 个答案:

答案 0 :(得分:0)

你需要:

1)在WCF中启用会话

2)使用WCF进行身份验证

3)继续重用代理而不是创建新代理。

这很有用: http://msdn.microsoft.com/en-us/library/ms733040.aspx