【问题标题】:RIA Services: How can I create custom authentication?RIA 服务:如何创建自定义身份验证?
【发布时间】:2009-07-28 16:46:22
【问题描述】:

我正在使用 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-3.0 wcf-ria-services


    【解决方案1】:

    如果您创建“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);
        }
    }
    

    【讨论】:

    • 谢谢,这就是我要找的。干杯
    • 正在寻找...我的黄油手指和我不介意 cmets 的编辑选项...
    • 请您更详细地解释一下这个答案吗?我真的很难实现正常的身份验证,我找不到任何不是完全模棱两可并且已经假设知识的良好信息来源。我真的很感激一些帮助。
    • 感谢您的回答。我想让我的 AuthenticationService 成为 LinqToEntitiesDomainService 子类,因此我不能从 AuthenticationBase 继承。实现 IAuthentication 接口的最佳实践是什么?
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      实现IAuthorization接口怎么样?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多