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

时间:2009-07-28 16:46:23

标签: 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);

我不知道如何实现其中任何一个(登录除外) - 该服务如何才能知道当前登录的用户是为了使Logout()工作?

我一直在网上搜索如何执行这几个小时,我找不到任何描述如何创建一个简单的DomainService,可用于在“RIA链接”中验证用户Silverlight项目。

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

谢谢,
查尔斯


[编辑]
我找到了RIA Services page on the MSDN Code Gallery。有一个名为Authentication Samples的部分,它链接到一些很棒的代码示例。如果您想了解有关身份验证在RIA服务中如何运作的更多信息,请查看它。

3 个答案:

答案 0 :(得分:20)

如果您创建“Silverlight业务应用程序”,您将看到模板如何实现身份验证。 (或者只是去here and download the template sample project。)

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

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

接下来,通过派生自UserBase:

在服务器端创建一个具体的User类
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);
    }
}

答案 1 :(得分:1)

答案 2 :(得分:0)

如何实现IAuthorization界面?