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

17

我正在使用Silverlight RIA Services 并且想创建自定义身份验证。但这似乎是唯一几乎没有文档的东西 (我已经阅读完整个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-linked” Silverlight项目中验证用户身份,但是没有找到任何描述这一点的内容。

如果有人能够解释一下,我将不胜感激。

谢谢,
查尔斯


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

3个回答

20
如果您创建一个“Silverlight Business Application”,您会看到该模板如何实现身份验证。(或者只需前往此处并下载模板示例项目。)
为了简化,这是我使用的过程:
首先,我创建了一个域服务(FooService),它派生自LinqToEntitiesDomainService,其中FooContext是我的实体模型。在其中,我添加了所有CRUD操作以访问我的自定义DB表并返回用户配置文件。
接下来,在服务器端通过从UserBase派生出来创建一个具体的用户类:
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);
    }
}

寻找... 我这双手笨拙,希望评论区能有一个编辑选项... - Charles
请您能详细解释一下这个答案吗?我真的很难实现普通的身份验证,也找不到任何好的信息来源,因为它们都含糊不清并且假定读者已经有相关知识。我非常感激得到一些帮助。 - Goober
谢谢你的回答。 我希望我的AuthenticationService成为LinqToEntitiesDomainService<MyEntitiesModel>的子类,因此我无法继承自AuthenticationBase。 在实现IAuthentication<T>接口方面,有什么最佳实践吗? - Shimmy Weitzhandler

1

0

你觉得实现 IAuthorization 接口怎么样?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接