RIA服务:如何创建自定义身份验证?

Cha*_*les 17 silverlight-3.0 wcf-ria-services

我正在使用Silverlight RIA服务,我想创建自定义身份验证.这似乎是唯一几乎没有文档的东西(我已经阅读了整个RIAServicesOverview.docx).

您知道我创建客户身份验证服务的方法吗?我不想使用默认的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);
Run Code Online (Sandbox Code Playgroud)

我不知道我将如何实现其中任何一个(登录除外) - 如果Logout()工作,服务怎么可能知道用户当前登录了什么?

我已经冲刷寻找如何做到这一点了几个小时在网上,我无法找到任何介绍如何创建一个简单的DomainService可以用于在"RIA联" Silverlight项目验证用户.

如果有人能够对此有所了解,我将非常感激.

谢谢,
查尔斯


[编辑]
在MSDN代码库中找到了RIA服务页面.有一个名为Authentication Samples的部分,它链接到一些很棒的代码示例.如果您想了解有关身份验证在RIA服务中如何工作的更多信息,请查看它.

Nic*_*tch 20

如果您创建"Silverlight业务应用程序",您将看到模板如何实现身份验证.(或者只是去这里下载模板示例项目.)

为简化起见,这是我使用的过程:

首先,我创建一个源自LinqToEntitiesDomainService的域服务(FooService),其中FooContext是我的实体模型.在其中我添加了所有CRUD操作来访问我的自定义数据库表并返回用户配置文件.

接下来,通过从UserBase派生,在服务器端创建一个具体的User类:

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

public class User : UserBase
{}
Run Code Online (Sandbox Code Playgroud)

最后,从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);
    }
}
Run Code Online (Sandbox Code Playgroud)