2009-07-28 57 views
17

我正在使用Silverlight RIA服務,我想創建自定義身份驗證。這似乎是唯一沒有文檔的東西(我已經閱讀了整個RIAServicesOverview.docx)。RIA服務:我如何創建自定義身份驗證?

您是否知道創建客戶身份驗證服務的方式?我不想使用默認的ASP.NET成員資格模型。我不知道我需要實現哪個接口或抽象類 - 儘管我找到了System.Web.Ria.ApplicationServices.IAuthentication。

我需要實現IAuthentication嗎?如果是這樣,你能給我一些關於如何去做的建議嗎?這是下面的方法:

public User GetUser(); 

    public User Login(string userName, string password, bool isPersistent, string customData); 

    public User Logout(); 

    public void UpdateUser(User user); 

我不知道我將如何實現任何這些(除登錄) - 如何可以在服務可能知道哪些用戶是當前登錄,以便退出()到工作?

我一直在搜尋網絡搜索如何做幾個小時,我找不到任何東西描述如何創建一個簡單的DomainService,可用於認證用戶在一個「RIA鏈接「Silverlight項目。

如果有人能夠對此有所瞭解,我會非常感激。

謝謝,
查爾斯


[編輯]
我發現RIA Services page on the MSDN Code Gallery。有一個名爲Authentication Samples的部分,鏈接到一些優秀的代碼示例。如果您想了解更多關於認證在RIA服務中如何工作的信息,請查看。

回答

20

如果您創建一個「Silverlight業務應用程序」,您將看到模板如何實現身份驗證。 (或者只是去here and download the template sample project。)

爲了簡化,這裏是我使用的過程:

首先,我創建一個從LinqToEntitiesDomainService派生那裏FooContext是我的實體模型域服務(FooService接口)。在其中添加所有CRUD操作以訪問我的自定義數據庫表並返回用戶配置文件。

接下來,從用​​戶羣獲得創建於服務器端的一個具體的用戶類:

using System.Web.Ria; 
using System.Web.Ria.ApplicationServices; 

public class User : UserBase 
{} 

最後,從AuthenticationBase派生類並實現以下四種方法:

[EnableClientAccess] 
public class AuthenticationService : AuthenticationBase<User> 
{ 
    private FooService _service = new FooService(); 

    protected override bool ValidateUser(string username, string password) 
    { 
     // Code here that tests only if the password is valid for the given 
     // username using your custom DB calls via the domain service you 
     // implemented above 
    } 

    protected override User GetAuthenticatedUser(IPrincipal pricipal) 
    { 
     // principal.Identity.Name will be the username for the user 
     // you're trying to authenticate. Here's one way to implement 
     // this: 
     User user = null; 
     if (this._service.DoesUserExist(principal.Identity.Name)) // DoesUserExist() is a call 
                    // added in my domain service 
     { 
      // UserProfile is an entity in my DB 
      UserProfile profile = this._service.GetUserProfile(principal.Identity.Name); 
      user.Name = profile.UserName; 
      user.AuthenticationType = principal.Identity.AuthenticationType; 
     } 
     return user; 
    } 

    public override void Initialize(DomainServiceContext context) 
    { 
     this._service.Initialize(context); 
     base.Initialize(context); 
    } 

    protected override void Dispose(bool disposing) 
    { 
     if (disposing) 
      this._service.Dispose(); 
     base.Dispose(disposing); 
    } 
} 
+0

謝謝,這就是我一直在尋找。歡呼 – Charles 2009-08-05 15:38:34

0

如何實現IAuthorization接口?

相關問題