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

发布于 2024-07-29 06:10:44 字数 1085 浏览 2 评论 0原文

我正在使用 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() 工作?

我在网上搜索了几个小时,寻找如何执行此操作,但找不到任何描述如何创建可用于在“RIA 链接”Silverlight 项目中对用户进行身份验证的简单 DomainService 的内容。

如果有人能阐明这一点,我将不胜感激。

谢谢,
查尔斯


[编辑]
我在 MSDN 代码库上找到了 RIA 服务页面。 有一个名为身份验证示例的部分,其中链接到一些很棒的代码示例。 如果您想了解有关 RIA 服务中身份验证如何工作的更多信息,请查看此内容。

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).

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.

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

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?

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.

Thanks,
Charles

[EDIT]
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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流年已逝 2024-08-05 06:10:44

如果您创建“Silverlight 业务应用程序”,您将看到模板如何实现身份验证。 (或者直接此处并下载模板示例项目。)

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

首先,我创建一个域服务 (FooService)派生自 LinqToEntitiesDomainService,其中 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);
    }
}

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:

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.

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
{}

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);
    }
}
晨曦慕雪 2024-08-05 06:10:44

如何实现IAuthorization接口?

How about implementing the IAuthorization interface?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文