微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在使用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服务中的身份验证如何工作,请查看。

解决方法

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

为了简化,我使用的过程如下:

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

接下来,通过从UserBase派生,在serverside上创建一个具体的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);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐