RIA 服务:如何创建自定义身份验证? [英] RIA Services: How can I create custom authentication?

查看:31
本文介绍了RIA 服务:如何创建自定义身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working with the Silverlight RIA Services and I want to create custom authentication. This appears to be the only thing that has virtually no documentation (I've read through the entire RIAServicesOverview.docx).

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

Do you know of a way for me to create a customer authentication service? I don't want to use the default ASP.NET membership model. I don't know what interface or abstract class I need to implement - although I did find System.Web.Ria.ApplicationServices.IAuthentication.

我需要实现 IAuthentication 吗?如果是这样,你能给我一些关于如何去做的建议吗?这些是以下方法:

Do I need to implement IAuthentication? If so, could you give me some advice on how to go about doing so? These are the following methods:

    public User GetUser();

    public User Login(string userName, string password, bool isPersistent, string customData);

    public User Logout();

    public void UpdateUser(User user);

我不知道如何实现其中的任何一个(登录除外)——服务怎么可能知道当前登录的是哪个用户以便 Logout() 工作?

I don't know how I would implement any of these (except for Login) - how could the service possibly know what user is currently logged in in order for Logout() to work?

数小时以来,我一直在网上搜索如何执行此操作,但找不到任何描述如何创建可用于在RIA 链接"中对用户进行身份验证的简单 DomainService 的内容Silverlight 项目.

I've been scouring the web in search of how to do this for hours, and I can't find anything that describes how to create a simple DomainService that can be used for authenticating a user in an "RIA-linked" Silverlight project.

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

If someone could shed some light on this, I'd be sincerely grateful.

谢谢,
查尔斯



我在 MSDN 代码库中找到了 RIA 服务页面.有一个名为 Authentication Samples 的部分,它链接到一些很棒的代码示例.如果您想详细了解 RIA 服务中的身份验证工作方式,请查看.



I found the RIA Services page on the MSDN Code Gallery. There's a section called Authentication Samples, which links to some great code samples. Check it out if you want to know more about how authentication works within RIA Services.

推荐答案

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

If you create a "Silverlight Business Application" you'll see how the template implements authentication. (Or just go here and download the template sample project.)

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

To simplify, here's the process I used:

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

First, I create a domain service (FooService) that derives from LinqToEntitiesDomainService where FooContext is my entity model. In it I add all the CRUD operations to access my custom DB table and return user profiles.

接下来,通过从 UserBase 派生在服务器端创建一个具体的 User 类:

Next, create a concrete User class on the serverside by deriving from UserBase:

using System.Web.Ria;
using System.Web.Ria.ApplicationServices;

public class User : UserBase
{}

最后,从AuthenticationBase派生一个类,并实现以下四个方法:

Finally, derive a class from AuthenticationBase and implement the following four methods:

[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);
    }
}

这篇关于RIA 服务:如何创建自定义身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆