昨天我通過移植現有的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