代码之家  ›  专栏  ›  技术社区  ›  Charles

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

  •  17
  • Charles  · 技术社区  · 17 年前

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

    你知道我创建客户认证服务的方法吗?我不想使用默认的ASP.NET成员资格模型。我不知道需要实现什么接口或抽象类——尽管我找到了System.Web.Ria.ApplicationServices.IAuthentication。

    我需要实现身份验证吗?如果是的话,你能给我一些建议吗?这些方法如下:

        public User GetUser();
    
        public User Login(string userName, string password, bool isPersistent, string customData);
    
        public User Logout();
    
        public void UpdateUser(User user);
    

    我不知道如何实现其中的任何一个(除了登录)——服务如何可能知道当前登录的是什么用户,以便注销()工作?

    我已经搜索了几个小时的网页,寻找如何做到这一点,我找不到任何描述如何创建一个简单的域服务,可用于验证“RIA链接”Silverlight项目中的用户。

    如果有人能对这件事有所了解,我将由衷地感激。

    谢谢,
    查尔斯


    [编辑]
    我找到了 RIA Services page on the MSDN Code Gallery . 有一个叫 Authentication Samples ,链接到一些伟大的代码示例。如果您想进一步了解RIA服务中身份验证的工作原理,请查看它。

    3 回复  |  直到 14 年前
        1
  •  20
  •   Nick Gotch    17 年前

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

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

    首先,我创建了一个从linqtoentiesdomainservice派生的域服务(fooservice),其中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);
        }
    }
    
        3
  •  0
  •   Jérôme Verstrynge    14 年前

    如何实施 IAuthorization 接口?

    推荐文章