17

我正在使用 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 项目中对用户进行身份验证。

如果有人能对此有所了解,我将不胜感激。

谢谢,
查尔斯


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

4

3 回答 3

20

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

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

首先,我创建了一个从 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);
    }
}
于 2009-08-05T14:31:04.850 回答
1

这是来自MS的完整官方示例:

http://code.msdn.microsoft.com/Custom-Authentication-96ca3d20

于 2012-02-19T15:07:24.750 回答
0

如何实现IAuthorization接口?

于 2010-05-28T07:40:28.080 回答