昨天我通过移植现有的ASP.NET Web服务开始学习WCF。WCF和身份验证
创建WCF服务本身非常简单。在创建我的第一个WCF服务库项目后大约一个小时,我已经成功地在WCF Test Client中测试了我的新WCF服务。
现在我想实现一个简单的认证系统,但仍然不知道如何。为了简单起见,说我的Web服务有三个操作:登录,获取用户名的长度和注销。我如何在以下代码中完成TODO?
[ServiceContract]
public class MyService
{
[OperationContract(IsInitiating = true, IsTerminating = false)]
public bool Login(string userName, string password)
{
/* I have already implemented the function that validades
whether the user name and password are correct. */
if (ValidateLogin(userName, password))
{
/* TODO: Initiate a session */
return true;
}
else
return false;
}
[OperationContract(IsInitiating = false, IsTerminating = false)]
public int GetUserNameLength()
{
/*
TODO: How to validate whether the user has logged in?
How to obtain the name of the user that has logged in?
*/
int userNameLength = 42;
return userNameLength;
}
[OperationContract(IsInitiating = false, IsTerminating = true)]
public void Logout()
{
/* TODO: How to logout? */
}
}
注:我的头号敌人总值黑客之一。请引导我走向概念上的“清洁”解决方案,无论其复杂程度如何。
@Sixto Saez:我一直在努力不去处理应用程序级别的身份验证。我编写了自己的'UserNamePasswordValidator'派生类,并相应地修改了我的'App.config'文件。它仍然没有工作。我会_love_能够分别处理认证和实际操作。 – pyon 2011-05-12 21:59:13
使用WCF配置自定义验证可能会很棘手。您需要提供更多关于您计划用于服务的绑定或绑定的详细信息。您选择的绑定将决定您将如何实现自定义验证。这篇[文章解释了如何配置](http://msdn.microsoft.com/zh-cn/library/ms731049.aspx)内置的ASP.NET成员资格提供程序与WCF一起使用。我将从文章中的示例代码开始,至少可以设置一个可用的WCF服务。接下来,使用该示例服务来编写您正在编写的服务。 – 2011-05-12 22:39:31
@Sixto Saez:我对实现全面的'MembershipProvider'不感兴趣。我只想实现一个'UserNamePasswordValidator'。根据文件,它应该工作。 – pyon 2011-05-12 23:20:36